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.AdMaterialDto;
import com.sptg.ad.api.entity.AdMaterial;
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.entity.Organization;
import com.sptg.permissions.api.feigns.ConfFeign;
import com.sptg.permissions.api.feigns.OrgFeign;
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.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/material")
@RequiredArgsConstructor
public class AdMaterialController {

    private final ConfFeign confFeign;
    private final SecurityServiceInterface<Account> securityServiceInterface;
    private final AdMaterialFeign adMaterialFeign;
    private final SysOperationLogFeign sysOperationLogFeign;
    private final OrgFeign orgFeign;


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

    /**
     * 查询广告素材列表(分页)
     * @param adMaterialDto 查询条件参数
     * @param page 分页参数
     * @return
     *
     *
     */
    @PostMapping(value = "/showAdMaterialList")
    @Security(token = AdSecurityToken.AD_MATERIAL_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<Page<AdMaterial>> showAdMaterialList(AdMaterialDto adMaterialDto, Page<AdMaterial> page){
        //获取当前登陆用户
//        Account onLineUser = this.securityServiceInterface.getOnLineUser();
//
//        adMaterialDto.setOrgId(onLineUser.getOrgId().toString());
//        adAdsenseDto.setOrgPosition(onLineUser.getOrgPosition());
        adMaterialDto.setPage(page);
        ResultBean<Page<AdMaterial>> pageResultBean = this.adMaterialFeign.showAdMaterialList(adMaterialDto);
        if(pageResultBean.getData() != null){
            pageResultBean.getData().getRecords().parallelStream().forEach(adMaterial -> {
                if (adMaterial.getOrgId() != null) {
                    Organization org = orgFeign.getOrgById(adMaterial.getOrgId());
                    if(null != org){
                        adMaterial.setOrgName(org.getName());
                    }
                }
            });
        }
        return pageResultBean;
    }

    /**
     * 功能描述:显示新增/编辑页面 iframe
     * @param
     * @return
     *
     *
     */
    @GetMapping("/add/view")
    @Security(token = AdSecurityToken.AD_MATERIAL_INSERT,format = DataFormatEnum.VIEW)
    public ModelAndView showEditAdMaterial(Long id){
        ModelAndView modelAndView = new ModelAndView("ad/ad-material-add");
        modelAndView.addObject("fastDfsServer", this.confFeign.getFileServerHost());
        AdMaterial adMaterial = new AdMaterial();
        //编辑
        if(id!=null){
           AdMaterial edit= this.adMaterialFeign.findAdMaterialById(id);
            if(adMaterial!=null) {
                adMaterial = edit;
            }
        }
        modelAndView.addObject("adMaterial",adMaterial);
        return modelAndView;
    }



    /**
     * 功能描述:新增/编辑广告素材
     * @param adMaterial
     * @return
     *
     *
     */
    @PostMapping (value = "/addAdMaterial")
    @Security(token = AdSecurityToken.AD_MATERIAL_INSERT,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> addAdMaterial(AdMaterial adMaterial){
        //获取当前登陆用户
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        int operationEvent= SysLogConstant.ALTER_EVENT;
        //新增
        if(adMaterial.getId()==null){
            adMaterial.setCreateAccountId(onLineUser.getId());
            adMaterial.setCreateAccountName(onLineUser.getLoginName());
            if(adMaterial.getOrgId()==null){
                adMaterial.setOrgId(onLineUser.getOrgId());
            }

            adMaterial.setCreateDate(new Date());
//            adContent.setRootOrgId(onLineUser.getRootOrgId());
            adMaterial.setStatus(OperationConstant.ZERO);
//            adContent.setOrgPosition(onLineUser.getOrgPosition());
            operationEvent= SysLogConstant.ADD_EVENT;
        }
        //保存
        ResultBean<Boolean> resultBean=this.adMaterialFeign.addAdMaterial(adMaterial);
        if(resultBean.getData()!=null && resultBean.getData()){
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            SysOperationLog sysOperationLog=new SysOperationLog(SysLogMenuConstant.AD_MATERIAL_MANAGE, operationEvent,
                    WebUtils.getIp(),adMaterial,onLineUser);
            sysOperationLogFeign.info(sysOperationLog);
            /*-------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }

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

    /**
     * 功能描述:批量删除广告素材
     * @param ids 选中的id集合
     * @return ResultBean<Boolean>
     *
     *
     */
    @PostMapping(value = "/deleteAdMaterialList")
    @Security(token = AdSecurityToken.AD_MATERIAL_DELETE,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> deleteAdMaterialList(@RequestBody List<String> ids){
        if (null == ids || ids.size() <=0){
            //  参数异常
            return new ResultBean<>(ResultEnum.PARAMETER_ANOMALY);
        }
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        AdMaterialDto dto = new AdMaterialDto();
        dto.setIds(ids);
        ResultBean<Boolean> resultBean =  this.adMaterialFeign.deleteAdMaterialList(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);
//    }
}
