package com.hzs.activity.consume.controller.manage;/**
 * @Description:
 * @Author: yuhui
 * @Time: 2023/4/22 10:16
 * @Classname: AcConsumeRuleConfigController
 * @PackageName: com.hzs.activity.consume.controller.manage
 */

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzs.activity.base.param.ActivityParam;
import com.hzs.activity.base.param.ConsumeRuleConfigParam;
import com.hzs.activity.base.param.QueryActivityParam;
import com.hzs.activity.base.service.IActivityService;
import com.hzs.activity.base.service.IAcBaseConfigService;
import com.hzs.activity.base.service.IAcGoalMemberConfigService;
import com.hzs.activity.base.vo.BaseConfigVo;
import com.hzs.activity.base.vo.ConsumeRuleConfigVo;
import com.hzs.activity.consume.service.IAcConsumeRuleConfigService;
import com.hzs.activity.consume.service.IAcConsumeShieldConfigService;
import com.hzs.activity.recommend.param.AcRecommendParam;
import com.hzs.activity.wares.service.IAcGiftConfigService;
import com.hzs.common.core.annotation.Log;
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.poi.ExcelUtil;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.domain.activity.base.AcBaseConfig;
import com.hzs.common.domain.activity.base.AcGiftConfig;
import com.hzs.common.domain.activity.base.AcGoalMemberConfig;
import com.hzs.common.domain.activity.base.ext.AcGiftConfigExt;
import com.hzs.common.domain.activity.consume.AcConsumeRuleConfig;
import com.hzs.common.domain.activity.consume.ext.AcConsumeShieldConfigExt;
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.ArrayList;
import java.util.List;

/**
 *@BelongsProject: hzs_cloud
 *@BelongsPackage: com.hzs.activity.consume.controller.manage
 *@Author: yh
 *@CreateTime: 2023-04-22  10:16
 *@Description: TODO
 *@Version: 1.0
 */
@RestController
@RequestMapping("/manage/consume-rule-config")
public class AcConsumeRuleConfigController extends BaseController {
    @Autowired
    private UserTokenService userTokenService;
    @Autowired
    private IAcConsumeRuleConfigService consumeRuleConfigService;
    @Autowired
    private IActivityService IActivityService;
    @Autowired
    private IAcBaseConfigService baseConfigService;
    @Autowired
    private IAcGoalMemberConfigService goalMemberConfigService;
    @Autowired
    private IAcGiftConfigService waresGiftConfigService;
    @Autowired
    private IAcConsumeShieldConfigService consumeShieldConfigService;
    /**
     * 新增消费赠送规格
     *
     * @return
     */
    @PostMapping("consumption-gift")
    public AjaxResult consumptionGift(@RequestBody  ActivityParam activityParam) {
        activityParam.setPkCountry(SecurityUtils.getPkCountry());
        activityParam.setPkCreator(SecurityUtils.getUserId());
        activityParam.setActType(EActType.CONSUMPTION_GIFT.getValue());
        IActivityService.saveConsumptionGift(activityParam);
        return AjaxResult.success();
    }

    /**
     * 消费赠送规格
     * @return
     */
    @PostMapping("update")
    public AjaxResult update(){

        return AjaxResult.success();
    }

    /**
     * 查询详情
     * @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<AcConsumeRuleConfig> qw = new QueryWrapper<>();
        qw.eq("PK_BASE_ID",baseConfig.getPkId());
        List<AcConsumeRuleConfig> consumeRuleConfigList = consumeRuleConfigService.list(qw);
        List<ConsumeRuleConfigParam> consumeRuleConfigParamList = new ArrayList<>();

        Integer orderType = null;
        for (AcConsumeRuleConfig acConsumeRuleConfig : consumeRuleConfigList) {
            List<ConsumeRuleConfigVo> ruleConfigList = new ArrayList<>();
            ConsumeRuleConfigVo cv = BeanUtil.copyProperties(acConsumeRuleConfig, ConsumeRuleConfigVo.class);
            QueryWrapper<AcGiftConfig> qe = new QueryWrapper<>();
            qe.eq("PK_RULE_ID",acConsumeRuleConfig.getPkId());
     //       List<AcGiftConfig> waresGiftConfigList = waresGiftConfigService.list(qe);
            List<AcGiftConfigExt> giftConfigList = waresGiftConfigService.queryListByRuleIdList(acConsumeRuleConfig.getPkId(),SecurityUtils.getPkCountry());
            cv.setWaresGiftConfigList(giftConfigList);
            ruleConfigList.add(cv);
            orderType = acConsumeRuleConfig.getOrderType();
            ConsumeRuleConfigParam consumeRuleConfigParam = new ConsumeRuleConfigParam();
            consumeRuleConfigParam.setOrderType(orderType);
            consumeRuleConfigParam.setConsumeRuleConfigList(ruleConfigList);
            consumeRuleConfigParamList.add(consumeRuleConfigParam);
        }

        activityParam.setConsumeRuleConfigParamList(consumeRuleConfigParamList);
//        QueryWrapper<AcConsumeShieldConfig> qr = new QueryWrapper<>();
//        qr.eq("PK_BASE_ID",baseConfig.getPkId());
        // TODO 产品名称
    //    List<AcConsumeShieldConfig> consumeShieldConfigList = consumeShieldConfigService.list(qr);
        List<AcConsumeShieldConfigExt> consumeShieldConfigList = consumeShieldConfigService.queryListByRuleIdList(baseConfig.getPkId());
        activityParam.setConsumeShieldConfigList(consumeShieldConfigList);
        return AjaxResult.success(activityParam);
    }


    /**
     * 删除
     * @param pkId
     * @return
     */
    @DeleteMapping("/{pkId}")
    public AjaxResult remove(@PathVariable Integer pkId) {

        consumeRuleConfigService.removeActivity(pkId);
        return AjaxResult.success();
    }


    /**
     * 消费赠送导出
     */
    @PostMapping("export")
    public void export (QueryActivityParam queryActivityParam, HttpServletResponse response){
        List<BaseConfigVo> resultList = IActivityService.activityList(queryActivityParam);
        ExcelUtil<BaseConfigVo> util = new ExcelUtil<>(BaseConfigVo.class);
        util.exportExcel(response, resultList, "商品赠送导出");
    }

    /**
     * 在线签呈
     *
     * @param recommendParam 消费赠送参数
     * @return AjaxResult
     */
    @Log(module = EOperationModule.CONSUME_GIVE, business = EOperationBusiness.CONSUME_GIVE, method = EOperationMethod.INSERT, remark = "在线签呈")
    @PostMapping("/online-petition")
    public AjaxResult onLinePetition(@RequestBody AcRecommendParam recommendParam) {
        recommendParam.setPkCountry(SecurityUtils.getPkCountry());
        recommendParam.setPkMember(SecurityUtils.getUserId());
        recommendParam.setLoginUser(userTokenService.getLoginUser());
        consumeRuleConfigService.onLinePetition(recommendParam);
        return AjaxResult.success();
    }

}
