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.AdTypeDto;
import com.sptg.ad.api.entity.AdType;
import com.sptg.ad.api.feigns.AdTypeFeign;
import com.sptg.common.core.beans.ResultBean;
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.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;


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

    private final AdTypeFeign adTypeFeign;
    private final SecurityServiceInterface<Account> securityServiceInterface;
    private final SysOperationLogFeign sysOperationLogFeign;

   /**
    * 功能描述:广告分类页面
    * @param
    * @return
    *               
    *                      
    */
    @GetMapping("/index")
    @Security(token = AdSecurityToken.AD_TYPE_MANAGE,format = DataFormatEnum.VIEW)
    public ModelAndView index(){
        return new ModelAndView("ad/ad-type-manage");
    }

    /**
     * 查询广告类型列表(分页)
     * @param adTypeDto 查询条件参数
     * @param page 分页参数
     * @return 
     *               
     *                      
     */
    @PostMapping (value = "/showAdTypeList")
    @Security(token = AdSecurityToken.AD_TYPE_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<Page<AdType>> showAdTypeList(AdTypeDto adTypeDto,Page<AdType> page){
        //  获取当前用户
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        adTypeDto.setRootOrgId(onLineUser.getRootOrgId());
        adTypeDto.setPage(page);
        return this.adTypeFeign.showAdTypeList(adTypeDto);
    }

    /**
     * 功能描述:显示添加分类页面 iframe
     * @param
     * @return
     *               
     *                      
     */
    @GetMapping("/ad-type-edit")
    @Security(token = AdSecurityToken.AD_TYPE_INSERT,format = DataFormatEnum.VIEW)
    public ModelAndView showAddType(){
        return new ModelAndView("ad/ad-type-edit");
    }

    /**
     * 功能描述:新增广告分类
     * @param
     * @return 
     *               
     *                      
     */
    @PostMapping (value = "/addAdType")
    @Security(token = AdSecurityToken.AD_TYPE_INSERT,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> addAdType(AdType adType){
        //  获取当前登陆用户
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        //日志事件
        int operationEvent= SysLogConstant.ALTER_EVENT;
        AdType oldObj = new AdType();
        //新增
        if(adType.getId()==null){
            adType.setCreateAccountId(onLineUser.getId());
            adType.setCreateAccountName(onLineUser.getLoginName());
            adType.setCreateDate(new Date());
            adType.setOrgId(onLineUser.getOrgId());
            adType.setRootOrgId(onLineUser.getRootOrgId());
            adType.setStatus(OperationConstant.ZERO);
            operationEvent= SysLogConstant.ADD_EVENT;
            oldObj = adType;
        }else {
            oldObj = this.adTypeFeign.getAdTypeById(adType.getId());
        }
        //保存
        ResultBean<Boolean>  resultBean=this.adTypeFeign.addAdType(adType);
        if(resultBean.getData()!=null && resultBean.getData()){
            AdType newObj = new AdType();
            if (adType.getId() != null){
                newObj = this.adTypeFeign.getAdTypeById(adType.getId());
            }
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            SysOperationLog sysOperationLog=new SysOperationLog(SysLogMenuConstant.AD_TYPE_MANAGE, operationEvent,
                    WebUtils.getIp(),adType,onLineUser,oldObj,newObj);
            sysOperationLogFeign.info(sysOperationLog);
            /*-------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }

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

    /**
     * 功能描述:根据rootOrgId获取所有广告分类
     * @param
     * @return
     *               
     *                       
     */
    @GetMapping(value = "/getAdTypeList")
    public ResultBean<AdType> getAdTypeList(){
        //  获取当前登陆用户
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        return this.adTypeFeign.getAdTypeList(onLineUser.getRootOrgId());
    }

    /**
     * 功能描述:校验类表名称是否重复
     * @param
     * @return
     *               
     *                       
     */
    @PostMapping (value = "/checkAdTypeName")
    public ResultBean<Boolean> checkAdTypeName(AdType adType){
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        adType.setRootOrgId(onLineUser.getRootOrgId());
        return this.adTypeFeign.checkAdTypeName(adType);
    }

    /**
     * 功能描述:获取广告分类名字---缓存中获取
     * @param
     * @return
     *               
     *                       
     */
    @GetMapping (value = "/getAdTypeById")
    public void getAdTypeById(Long id){
        AdType adType = this.adTypeFeign.getAdTypeById(id);
        System.out.println(adType.getName());
    }
}
