package com.hzs.activity.recoilGive.controller.manage;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzs.activity.base.param.ActivityParam;
import com.hzs.activity.base.param.QueryActivityParam;
import com.hzs.activity.base.service.IAcBaseConfigService;
import com.hzs.activity.base.service.IAcGoalMemberConfigService;
import com.hzs.activity.base.service.IActivityService;
import com.hzs.activity.base.vo.BaseConfigVo;
import com.hzs.activity.recoilGive.service.IAcRecoilGiveConfigService;
import com.hzs.activity.recoilGive.vo.RecoilGiveConfigVO;
import com.hzs.activity.recommend.param.AcRecommendParam;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.msg.ConfigMsgConstants;
import com.hzs.common.core.enums.EActType;
import com.hzs.common.core.enums.EOperationBusiness;
import com.hzs.common.core.enums.EOperationMethod;
import com.hzs.common.core.enums.EOperationModule;
import com.hzs.common.core.utils.TransactionUtils;
import com.hzs.common.core.utils.poi.ExcelUtil;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.activity.base.AcBaseConfig;
import com.hzs.common.domain.activity.base.AcGoalMemberConfig;
import com.hzs.common.domain.activity.recoilGive.AcRecoilGiveConfig;
import com.hzs.common.security.service.UserTokenService;
import com.hzs.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Description: 复消币赠送活动配置 控制器
 * @Author: jiang chao
 * @Time: 2024/7/18 14:05
 * @Classname: AcRecoilGiveConfigController
 * @PackageName: com.hzs.activity.recoilGive.controller
 */
@RestController
@RequestMapping("/manage/ac-recoil-give")
public class AcRecoilGiveConfigController extends BaseController {
    @Autowired
    private IAcBaseConfigService baseConfigService;
    @Autowired
    private IAcGoalMemberConfigService goalMemberConfigService;
    @Autowired
    private IAcRecoilGiveConfigService iAcRecoilGiveConfigService;
    @Autowired
    private IActivityService activityService;
    @Autowired
    private UserTokenService userTokenService;

    /**
     * 查看配置列表
     *
     * @param pkBaseConfig 活动配置ID
     * @return
     */
    @GetMapping("/config-list")
    public AjaxResult configList(@RequestParam("PK_BASE_CONFIG") Long pkBaseConfig) {
        LambdaQueryWrapper<AcRecoilGiveConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AcRecoilGiveConfig::getPkBaseConfig, pkBaseConfig);
        iAcRecoilGiveConfigService.list(queryWrapper);


        return AjaxResult.success();
    }


    /**
     * 新增 复消币赠送活动配置
     */
    @PostMapping("save-recoil-give-config")
    public AjaxResult saveRecoilGiveConfig(@RequestBody ActivityParam activityParam){
        activityParam.setPkCountry(SecurityUtils.getPkCountry());
        activityParam.setPkCreator(SecurityUtils.getUserId());
        activityParam.setActType(EActType.RECOIL_GIVE.getValue());
        iAcRecoilGiveConfigService.saveRecoilGiveConfig(activityParam);
        return AjaxResult.success();
    }
    /**
     * 在线签呈
     *
     * @param recommendParam 复消币赠送活动配置
     * @return AjaxResult
     */
    @Log(module = EOperationModule.RECOIL_GIVE, business = EOperationBusiness.RECOIL_GIVE, method = EOperationMethod.INSERT, remark = "在线签呈")
    @PostMapping("/online-petition")
    public AjaxResult onLinePetition(@RequestBody AcRecommendParam recommendParam) {
        if (checkGradeRepeat(recommendParam.getActivityParam().getRecoilGiveConfigList())){
            return AjaxResult.error(TransactionUtils.getContent(TransactionUtils.getContent(ConfigMsgConstants.CONFIG_ACTIVITY_GRADE_NOT_NULL)));
        }


        recommendParam.setPkCountry(SecurityUtils.getPkCountry());
        recommendParam.setPkMember(SecurityUtils.getUserId());
        recommendParam.setLoginUser(userTokenService.getLoginUser());
        iAcRecoilGiveConfigService.onLinePetition(recommendParam);
        return AjaxResult.success();
    }

    /**
     * 校验数组中的等级是否重复
     */
    private Boolean checkGradeRepeat(List<RecoilGiveConfigVO> recoilGiveConfigList){

        Set<Integer> buySet = new HashSet<>();
        if (recoilGiveConfigList == null){
            return false;
        }
        for (RecoilGiveConfigVO recoilGiveConfigVO : recoilGiveConfigList) {

            buySet.add(recoilGiveConfigVO.getPkGrade());
        }
        if ( buySet.size() != recoilGiveConfigList.size()){
           return true;
        }
        return false;
    }

    /**
     * 复消币赠送列表查询
     * @param queryActivityParam
     * @return
     */
    @GetMapping("list")
    public TableDataInfo list(QueryActivityParam queryActivityParam){
        startPage();
        queryActivityParam.setActType(EActType.RECOIL_GIVE.getValue());
        queryActivityParam.setPkCountry(SecurityUtils.getPkCountry());
        List<BaseConfigVo> list = iAcRecoilGiveConfigService.queryList(queryActivityParam);
        return getDataTable(list);
    }
    /**
     * 复消币赠送列表导出
     * @param queryActivityParam
     * @return
     */
    @PostMapping("export")
    public void export(QueryActivityParam queryActivityParam, HttpServletResponse response){
        queryActivityParam.setActType(EActType.RECOIL_GIVE.getValue());
        queryActivityParam.setPkCountry(SecurityUtils.getPkCountry());
        List<BaseConfigVo> list = iAcRecoilGiveConfigService.queryList(queryActivityParam);
        ExcelUtil<BaseConfigVo> util = new ExcelUtil<>(BaseConfigVo.class);
        util.exportExcel(response, list, "活动配置导出");
    }

    /**
     * 详情
     * @param pkId
     * @return
     */
    @GetMapping("detail")
    public AjaxResult detail(Long pkId){
        // 基础表查询
        AcBaseConfig baseConfig = baseConfigService.getById(pkId);
        ActivityParam activityParam = BeanUtil.copyProperties(baseConfig, ActivityParam.class);
        //查询目标群体
        QueryWrapper<AcGoalMemberConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PK_BASE_ID",baseConfig.getPkId());
        List<AcGoalMemberConfig> goalMemberConfigList = goalMemberConfigService.list(queryWrapper);
        activityParam.setGoalMemberConfigList(goalMemberConfigList);
        QueryWrapper<AcRecoilGiveConfig> qw = new QueryWrapper<>();
        qw.eq("PK_BASE_CONFIG",pkId);
        List<AcRecoilGiveConfig> giveList = iAcRecoilGiveConfigService.list(qw);
        List<RecoilGiveConfigVO> giveConfigVOList = giveList.stream().map(a -> {
            return BeanUtil.copyProperties(a, RecoilGiveConfigVO.class);
        }).collect(Collectors.toList());
        activityParam.setRecoilGiveConfigList(giveConfigVOList);
        return AjaxResult.success(activityParam);
    }
}
