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

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.AcMemberConsumeRuleVo;
import com.hzs.activity.base.vo.BaseConfigVo;
import com.hzs.activity.consume.service.IAcMemberConsumeRuleService;
import com.hzs.activity.wares.service.IAcGiftConfigService;
import com.hzs.common.core.annotation.Log;
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.AcMemberConsumeRule;
import com.hzs.common.domain.activity.consume.ext.AcMemberConsumeRuleExt;
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;

/**
 * @description: 会员会员消费赠送活动配置
 * @author: zhang jing
 * @date: 2024/11/28 11:28
 * @param:
 * @return:
 **/
@RestController
@RequestMapping("/manage/member-consume-rule")
public class AcMemberConsumeRuleController extends BaseController {

    @Autowired
    private UserTokenService userTokenService;
    @Autowired
    private IAcMemberConsumeRuleService iAcMemberConsumeRuleService;
    @Autowired
    private IActivityService iActivityService;
    @Autowired
    private IAcBaseConfigService baseConfigService;
    @Autowired
    private IAcGoalMemberConfigService goalMemberConfigService;
    @Autowired
    private IAcGiftConfigService waresGiftConfigService;


    /**
     * 查询详情
     *
     * @param pkId
     * @return
     */
    @GetMapping("/detail")
    public AjaxResult detail(Long pkId) {
        // 基础表查询
        AcBaseConfig baseConfig = baseConfigService.getById(pkId);
        if(baseConfig!=null){
            ActivityParam activityParam = BeanUtil.copyProperties(baseConfig, ActivityParam.class);
            //查询目标群体
            LambdaQueryWrapper<AcGoalMemberConfig> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(AcGoalMemberConfig::getPkBaseId, baseConfig.getPkId());
            List<AcGoalMemberConfig> goalMemberConfigList = goalMemberConfigService.list(queryWrapper);
            activityParam.setGoalMemberConfigList(goalMemberConfigList);
            // 会员消费赠送规格配置
            LambdaQueryWrapper<AcMemberConsumeRule> qw = new LambdaQueryWrapper<>();
            qw.eq(AcMemberConsumeRule::getPkBaseId, baseConfig.getPkId());
            List<AcMemberConsumeRule> consumeRuleConfigList = iAcMemberConsumeRuleService.list(qw);
            List<AcMemberConsumeRuleExt>  mcrList=new ArrayList<>();
            for (AcMemberConsumeRule acMemberConsumeRule : consumeRuleConfigList) {
                AcMemberConsumeRuleExt cv = BeanUtil.copyProperties(acMemberConsumeRule, AcMemberConsumeRuleExt.class);
                LambdaQueryWrapper<AcGiftConfig> qe = new LambdaQueryWrapper<>();
                qe.eq(AcGiftConfig::getPkRuleId, acMemberConsumeRule.getPkId());
                List<AcGiftConfigExt> giftConfigList = waresGiftConfigService.queryListByRuleIdList(acMemberConsumeRule.getPkId(), SecurityUtils.getPkCountry());
                cv.setWgcList(giftConfigList);
                mcrList.add(cv);
            }
            activityParam.setCmrList(mcrList);
            return AjaxResult.success(activityParam);
        }else{
            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 memberConsumeRuleVo 会员消费赠送参数
     * @return AjaxResult
     */
    @Log(module = EOperationModule.CONSUME_GIVE, business = EOperationBusiness.CONSUME_GIVE, method = EOperationMethod.INSERT, remark = "在线签呈")
    @PostMapping("/online-petition")
    public AjaxResult onLinePetition(@RequestBody AcMemberConsumeRuleVo memberConsumeRuleVo) {
        memberConsumeRuleVo.setLoginUser(userTokenService.getLoginUser());
        iAcMemberConsumeRuleService.onLinePetition(memberConsumeRuleVo);
        return AjaxResult.success();
    }

}
