package com.sptg.gateway.controller.ad;

import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.ad.api.dto.AdPushAppDto;
import com.sptg.ad.api.entity.AdContent;
import com.sptg.ad.api.entity.AdPushApp;
import com.sptg.ad.api.entity.AdType;
import com.sptg.ad.api.feigns.AdContentFeign;
import com.sptg.ad.api.feigns.AdPushAppFeign;
import com.sptg.ad.api.feigns.AdTypeFeign;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.common.core.constant.SysLogConstant;
import com.sptg.common.core.util.WebUtils;
import com.sptg.common.security.annotation.Security;
import com.sptg.common.security.enums.DataFormatEnum;
import com.sptg.common.security.service.SecurityServiceInterface;
import com.sptg.permissions.api.entity.Account;
import com.sptg.permissions.api.feigns.ConfFeign;
import com.sptg.statistics.api.constant.SysLogMenuConstant;
import com.sptg.statistics.api.entity.SysOperationLog;
import com.sptg.statistics.api.feigns.SysOperationLogFeign;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

/**
 * title: AdPushAppController
 * package: com.sptg.estate.biz.service
 * description: 移动端广告-控制器
 *
 *               
 *                     
 */
@Slf4j
@RestController
@RequestMapping(value = "/ad/push/app")
@RequiredArgsConstructor
public class AdPushAppController {

    private final SecurityServiceInterface<Account> securityServiceInterface;
    private final SysOperationLogFeign sysOperationLogFeign;
    private final AdPushAppFeign adPushAppFeign;
    private final AdContentFeign adContentFeign;
    private final AdTypeFeign adTypeFeign;
    private final ConfFeign confFeign;
    /**
     * 功能描述:分页
     * @param
     * @return
     *               
     *                    
     */
    @GetMapping("/index")
    @Security(token = {"ad:push:app:manage"},format = DataFormatEnum.VIEW)
    public ModelAndView index(){
        return new ModelAndView("ad/ad-push-app-manage");
    }

    /**
     * 分页(分页)
     * @param dto 查询条件参数
     * @param page 分页参数
     * @return
     *               
     *                     
     */
    @PostMapping(value = "/showList")
    @Security(token = {"ad:push:app:manage"},format = DataFormatEnum.JSON)
    public ResultBean<Page<AdPushApp>> showList(AdPushAppDto dto, Page<AdPushApp> page){
        //  获取当前用户
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        dto.setOrgPosition(onLineUser.getOrgPosition());
        dto.setPage(page);
        return this.adPushAppFeign.showList(dto);
    }

    /**
     * 功能描述:新增
     * @return ModelAndView
     *               
     *                     
     */
    @GetMapping("/add/view")
    @Security(token = {"ad:push:app:add"},format = DataFormatEnum.VIEW)
    public ModelAndView addView(){
        return new ModelAndView("ad/ad-push-app-add");
    }

    /**
     *  新增
     * @param adPushApp
     * @return
     *               
     *                     
     */
    @PostMapping(value = "/add")
    @Security(token = {"ad:push:app:add"},format = DataFormatEnum.JSON)
    public ResultBean<Boolean> add(AdPushApp adPushApp){
        //  获取当前用户
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        adPushApp.setCreateAccountId(onLineUser.getId());
        adPushApp.setCreateAccountName(onLineUser.getLoginName());
        adPushApp.setCreateDate(new Date());
        adPushApp.setOrgId(onLineUser.getOrgId());
        adPushApp.setRootOrgId(onLineUser.getRootOrgId());
        adPushApp.setOrgPosition(onLineUser.getOrgPosition());

        AdContent adContent = this.adContentFeign.findAdContentById(adPushApp.getAdId());
        adPushApp.setAdCode(adContent.getAdCode());
        adPushApp.setAdId(adContent.getId());
        adPushApp.setAdTitle(adContent.getAdTitle());
        adPushApp.setAdTypeId(adContent.getAdTypeId());
        AdType adType = this.adTypeFeign.getAdTypeById(adContent.getAdTypeId());
        adPushApp.setAdTypeName(adType.getName());
        adPushApp.setIsEnable(OperationConstant.ZERO);
        adPushApp.setAppType(OperationConstant.ZERO);
        adPushApp.setStatus(OperationConstant.ZERO);
        if(StringUtils.isNotBlank(adContent.getVideoUri())){
            adPushApp.setVideoUri(this.confFeign.getFileServerHost() + adContent.getVideoUri());
            adPushApp.setVideoMd5(adContent.getVideoMd5());
            adPushApp.setVideoType(OperationConstant.ONE);
        }else if(adContent.getAdContentImgs()!=null && adContent.getAdContentImgs().size()>0){
            adPushApp.setVideoUri(this.confFeign.getFileServerHost() + adContent.getAdContentImgs().get(0).getImgUri());
            adPushApp.setVideoMd5(adContent.getAdContentImgs().get(0).getImgMd5());
            adPushApp.setVideoType(OperationConstant.ZERO);
        }else{
            return new ResultBean<>(ResultEnum.SUCCESS,false);
        }
        ResultBean<Boolean> resultBean = this.adPushAppFeign.addEntity(adPushApp);
        if( null != resultBean.getData() && resultBean.getData()){
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            SysOperationLog sysOperationLog=new SysOperationLog(SysLogMenuConstant.AD_TYPE_MANAGE, SysLogConstant.ADD_EVENT,
                    WebUtils.getIp(),adPushApp,onLineUser,adPushApp);
            sysOperationLogFeign.info(sysOperationLog);
            /*------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }

    /**
     * 功能描述: 开启或禁用账号信息
     * @param dto
     * {
     *     id       :   被禁用的账号Id
     *     isEnable :   { 1：开启，2：禁用 }
     * }
     * @return ResultBean<Boolean>
     *               
     *                     
     */
    @PostMapping(value = "/updateIsEnable")
    public ResultBean<Boolean> updateIsEnable(AdPushAppDto dto){
        Assert.notNull(dto,"非法参数");
        Assert.notNull(dto.getId(),"Id不能为空");
        Assert.notNull(dto.getIsEnable(),"请设置广告开启或禁用");
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        ResultBean<AdPushApp> oldObj = this.adPushAppFeign.findById(dto.getId());
        ResultBean<Boolean> resultBean=this.adPushAppFeign.updateIsEnable(dto);
        if( null != resultBean.getData() && resultBean.getData()){
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            SysOperationLog sysOperationLog=new SysOperationLog(SysLogMenuConstant.AD_TYPE_MANAGE, SysLogConstant.ALTER_EVENT,
                    WebUtils.getIp(),dto,onLineUser,oldObj);
            sysOperationLogFeign.info(sysOperationLog);
            /*------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }

    /**
     * 功能描述:删除--软删除
     * @param id
     * @return
     *               
     *                      
     */
    @GetMapping(value = "/delete/{id}")
    @Security(token = {"ad:push:app:delete"},format = DataFormatEnum.JSON)
    public ResultBean<Boolean> deleteAdType(@PathVariable(value = "id")Long id){
        ResultBean<Boolean>  resultBean=this.adPushAppFeign.delete(id);
        return resultBean;
    }

    /**
     * 功能描述:编辑
     * @return ModelAndView
     *               
     *                     
     */
    @GetMapping("/edit/view")
    @Security(token = {"ad:push:app:edit"},format = DataFormatEnum.VIEW)
    public ModelAndView showTaskAddContent(Long id){
        ModelAndView modelAndView = new ModelAndView("ad/ad-push-app-edit");
        ResultBean<AdPushApp> resultBean = this.adPushAppFeign.findById(id);
        modelAndView.addObject("adPushApp", resultBean.getData());
        return modelAndView;
    }

    /**
     *  编辑
     * @param adPushApp
     * @return
     *               
     *                     
     */
    @PostMapping(value = "/edit")
    @Security(token = {"ad:push:app:edit"},format = DataFormatEnum.JSON)
    public ResultBean<Boolean> edit(AdPushApp adPushApp){
        AdContent adContent = this.adContentFeign.findAdContentById(adPushApp.getAdId());
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        adPushApp.setAdCode(adContent.getAdCode());
        adPushApp.setAdId(adContent.getId());
        adPushApp.setAdTitle(adContent.getAdTitle());
        adPushApp.setAdTypeId(adContent.getAdTypeId());
        AdType adType = this.adTypeFeign.getAdTypeById(adContent.getAdTypeId());
        adPushApp.setAdTypeName(adType.getName());
        if(StringUtils.isNotBlank(adContent.getVideoUri())){
            adPushApp.setVideoUri(this.confFeign.getFileServerHost() + adContent.getVideoUri());
            adPushApp.setVideoMd5(adContent.getVideoMd5());
            adPushApp.setVideoType(OperationConstant.ONE);
        }else if(adContent.getAdContentImgs()!=null && adContent.getAdContentImgs().size()>0){
            adPushApp.setVideoUri(this.confFeign.getFileServerHost() + adContent.getAdContentImgs().get(0).getImgUri());
            adPushApp.setVideoMd5(adContent.getAdContentImgs().get(0).getImgMd5());
            adPushApp.setVideoType(OperationConstant.ZERO);
        }else{
            return new ResultBean<>(ResultEnum.SUCCESS,false);
        }
        ResultBean<Boolean> resultBean = this.adPushAppFeign.editById(adPushApp);
        if( null != resultBean.getData() && resultBean.getData()){
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            SysOperationLog sysOperationLog=new SysOperationLog(SysLogMenuConstant.AD_TYPE_MANAGE, SysLogConstant.ALTER_EVENT,
                    WebUtils.getIp(),adPushApp,onLineUser,adContent,adPushApp);
            sysOperationLogFeign.info(sysOperationLog);
            /*------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }

}
