package com.sptg.gateway.controller.ad;

import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.ad.api.constant.AdSecurityToken;
import com.sptg.ad.api.dto.AdContentDto;
import com.sptg.ad.api.entity.AdAdsense;
import com.sptg.ad.api.entity.AdContent;
import com.sptg.ad.api.entity.AdMaterial;
import com.sptg.ad.api.feigns.AdAdsenseFeign;
import com.sptg.ad.api.feigns.AdContentFeign;
import com.sptg.ad.api.feigns.AdMaterialFeign;
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.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * title: AdContentController
 * package: com.sptg.gateway.controller.ad
 * description: 广告类容-控制器
 */
@Slf4j
@RestController
@RequestMapping(value = "/ad/content")
@RequiredArgsConstructor
public class AdContentController {

    private final ConfFeign confFeign;
    private final SecurityServiceInterface<Account> securityServiceInterface;
    private final AdContentFeign adContentFeign;
    private final AdMaterialFeign adMaterialFeign;
    private final SysOperationLogFeign sysOperationLogFeign;
    private final AdAdsenseFeign adAdsenseFeign;


    /**
     * 功能描述:广告类容页面
     *
     * @param
     * @return
     */
    @GetMapping("/index")
    @Security(token = AdSecurityToken.AD_CONTENT_MANAGE, format = DataFormatEnum.VIEW)
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView("ad/ad-content-manage");
        modelAndView.addObject("fastDfsServer", this.confFeign.getFileServerHost());
        return modelAndView;
    }

    /**
     * 查询广告类容列表(分页)
     *
     * @param adContentDto 查询条件参数
     * @param page         分页参数
     * @return
     */
    @PostMapping(value = "/showAdContentList")
    @Security(token = AdSecurityToken.AD_CONTENT_MANAGE, format = DataFormatEnum.JSON)
    public ResultBean<Page<AdContent>> showAdContentList(AdContentDto adContentDto, Page<AdContent> page) {
        //获取当前登陆用户
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        adContentDto.setOrgPosition(onLineUser.getOrgPosition());
        adContentDto.setPage(page);
        return this.adContentFeign.showAdContentList(adContentDto);
    }

    /**
     * 功能描述:显示新增/编辑页面 iframe
     *
     * @param
     * @return
     */
    @GetMapping("/edit/view")
    @Security(token = AdSecurityToken.AD_CONTENT_INSERT, format = DataFormatEnum.VIEW)
    public ModelAndView showEditContent(Long id) {
        ModelAndView modelAndView = new ModelAndView("ad/ad-content-edit");
        modelAndView.addObject("fastDfsServer", this.confFeign.getFileServerHost());
        AdContent adContent = new AdContent();
        //编辑
        if (id != null) {
            AdContent edit = this.adContentFeign.findAdContentById(id);

            if (edit != null) {
                adContent = edit;
                Long materialId = edit.getAdMaterialId();
                AdMaterial adMaterial = this.adMaterialFeign.findAdMaterialById(materialId);
                if (null != adMaterial) {
                    adContent.setMaterialType(adMaterial.getMaterialType());
                    adContent.setMaterialName(adMaterial.getMaterialName());
                    adContent.setAdMaterialFile(adMaterial.getMaterialFile());
                }

            }
        }
        modelAndView.addObject("adContent", adContent);
        return modelAndView;
    }

    /**
     * 功能描述:新增/编辑广告类容
     *
     * @param adContent
     * @return
     */
    @PostMapping(value = "/addAdContent")
    @Security(token = AdSecurityToken.AD_CONTENT_INSERT, format = DataFormatEnum.JSON)
    public ResultBean<Boolean> addAdContent(AdContent adContent) {
        //获取当前登陆用户
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        int operationEvent = SysLogConstant.ALTER_EVENT;
        //新增
        if (adContent.getId() == null) {
            adContent.setCreateAccountId(onLineUser.getId());
            adContent.setCreateAccountName(onLineUser.getLoginName());
            adContent.setOrgId(onLineUser.getOrgId());
            adContent.setCreateDate(new Date());
            adContent.setRootOrgId(onLineUser.getRootOrgId());
            adContent.setStatus(OperationConstant.ZERO);
            adContent.setOrgPosition(onLineUser.getOrgPosition());
            operationEvent = SysLogConstant.ADD_EVENT;
        }
        //判断是否关联素材
        if (adContent.getAdMaterialId() != null) {
            adContent.setHasMaterial(1);
        }
        //判断关联的广告位是线上还是线上还是线下
        if (adContent.getAdsenseId() != null) {
            AdAdsense adsense = adAdsenseFeign.findAdsenseById(adContent.getAdsenseId());
            String adsenseType = adsense.getAdsenseType();
            if (adsenseType.equals("设备")) {
                adContent.setOnlineOrOffline(1);
            } else {
                adContent.setOnlineOrOffline(0);
            }
        }
        //保存
        ResultBean<Boolean> resultBean = this.adContentFeign.addAdContent(adContent);
        if (resultBean.getData() != null && resultBean.getData()) {
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            SysOperationLog sysOperationLog = new SysOperationLog(SysLogMenuConstant.AD_CONTENT_MANAGE, operationEvent,
                    WebUtils.getIp(), adContent, onLineUser);
            sysOperationLogFeign.info(sysOperationLog);
            /*-------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }

    /**
     * 功能描述:删除--软删除
     *
     * @param ids 广告类型id字符串，逗号隔开 1,2,3
     * @return
     */
    @GetMapping(value = "/deleteAdContent/{id}")
    @Security(token = AdSecurityToken.AD_CONTENT_DELETE, format = DataFormatEnum.JSON)
    public ResultBean<Boolean> deleteAdContent(@PathVariable(value = "id") String ids) {
        ResultBean<Boolean> resultBean = this.adContentFeign.deleteAdContent(ids);
        if (resultBean.getData()) {
            //获取当前登陆用户
            Account onLineUser = this.securityServiceInterface.getOnLineUser();
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            String[] idsArray = ids.split(",");
            if (idsArray.length > 0) {
                AdContentDto adContentDto = new AdContentDto();
                adContentDto.setIds(Arrays.asList(idsArray));
                SysOperationLog sysOperationLog = new SysOperationLog(SysLogMenuConstant.AD_CONTENT_MANAGE, SysLogConstant.DELETE_EVENT, WebUtils.getIp(), adContentDto, onLineUser);
                sysOperationLogFeign.info(sysOperationLog);
            }
            /*-------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }

    /**
     * 功能描述:批量删除账号
     *
     * @param ids 选中的id集合
     * @return ResultBean<Boolean>
     */
    @PostMapping(value = "/deleteAdContentList")
    @Security(token = AdSecurityToken.AD_CONTENT_DELETE, format = DataFormatEnum.JSON)
    public ResultBean<Boolean> deleteAdContentList(@RequestBody List<String> ids) {
        if (null == ids || ids.size() <= 0) {
            //  参数异常
            return new ResultBean<>(ResultEnum.PARAMETER_ANOMALY);
        }
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        AdContentDto dto = new AdContentDto();
        dto.setIds(ids);
        ResultBean<Boolean> resultBean = this.adContentFeign.deleteAdContentList(dto);
        if (null != resultBean.getData() && resultBean.getData()) {
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            SysOperationLog sysOperationLog = new SysOperationLog(SysLogMenuConstant.ACCOUNT_MANAGE, SysLogConstant.DELETE_EVENT,
                    WebUtils.getIp(), dto, onLineUser);
            sysOperationLogFeign.info(sysOperationLog);
            /*-------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }

    /**
     * 功能描述:模糊查询获取广告列表
     *
     * @param
     * @return
     */
    @GetMapping(value = "/getList")
    public ResultBean<AdContent> getList(AdContentDto dto) {
        if (null == dto || StringUtils.isEmpty(dto.getAdTitle())) {
            return new ResultBean<>(ResultEnum.PARAMETER_ANOMALY);
        }
        //  获取当前用户
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        dto.setOrgPosition(onLineUser.getOrgPosition());
        return this.adContentFeign.getList(dto);
    }
}
