package com.hzs.activity.wares.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.param.WaresConfigParam;
import com.hzs.activity.base.param.WaresRuleConfigParam;
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.recommend.param.AcRecommendParam;
import com.hzs.activity.wares.service.IAcGiftConfigService;
import com.hzs.activity.wares.service.IAcWaresRuleConfigService;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.enums.*;
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.wares.AcWaresRuleConfig;
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.wares.controller.manage
 * @Author: yh
 * @CreateTime: 2023-04-22  10:18
 * @Description: TODO
 * @Version: 1.0
 */
@RestController
@RequestMapping("/manage/wares-rule-config")
public class WaresRuleConfigController extends BaseController {

    @Autowired
    private UserTokenService userTokenService;
    @Autowired
    private IAcGoalMemberConfigService goalMemberConfigService;
    @Autowired
    private IActivityService iActivityService;
    @Autowired
    private IAcBaseConfigService baseConfigService;
    @Autowired
    private IAcGiftConfigService waresGiftConfigService;
    @Autowired
    private IAcWaresRuleConfigService waresRuleConfigService;

    /**
     * 新增商品赠送规格
     *
     * @return
     */
    @PostMapping("save-activity")
    public AjaxResult saveActivity(@RequestBody ActivityParam activityParam) {
        activityParam.setPkCountry(SecurityUtils.getPkCountry());
        activityParam.setPkCreator(SecurityUtils.getUserId());
        activityParam.setActType(EActType.PRODUCT_GIFT.getValue());
        Long pkbaseId = iActivityService.saveActivity(activityParam);
        //日志
        iActivityService.createBaseConfigLog(activityParam, new StringBuffer(), pkbaseId, EApprovalBusiness.PRODUCT_GIFT_ADD.getValue(), true);
        return AjaxResult.success();
    }

    /**
     * 商品赠送
     */
    @GetMapping("get-detail")
    public AjaxResult getDetail(Integer pkId) {
        AcBaseConfig baseConfig = baseConfigService.getById(pkId);
        ActivityParam activityParam = BeanUtil.copyProperties(baseConfig, ActivityParam.class);
        LambdaQueryWrapper<AcWaresRuleConfig> qe = new LambdaQueryWrapper<>();
        qe.eq(AcWaresRuleConfig::getPkBaseId, baseConfig.getPkId());

        List<AcWaresRuleConfig> waresRuleConfigList = waresRuleConfigService.list(qe);
        Integer orderType = null;
        List<WaresConfigParam> waresConfigParamList = new ArrayList<>();

        List<WaresRuleConfigParam> waresRuleConfigParam = new ArrayList<>();

        for (AcWaresRuleConfig acWaresRuleConfig : waresRuleConfigList) {

            LambdaQueryWrapper<AcGiftConfig> qr = new LambdaQueryWrapper<>();
            qr.eq(AcGiftConfig::getPkRuleId, acWaresRuleConfig.getPkId());
            List<AcGiftConfigExt> giftConfigList = waresGiftConfigService.queryListByRuleIdList(acWaresRuleConfig.getPkId(), SecurityUtils.getPkCountry());

            if (!acWaresRuleConfig.getOrderType().equals(orderType)) {
                waresRuleConfigParam = new ArrayList<>();

                orderType = acWaresRuleConfig.getOrderType();
                WaresConfigParam waresConfigParam = new WaresConfigParam();
                waresConfigParam.setOrderType(orderType);
                waresConfigParam.setWaresRuleConfigParam(waresRuleConfigParam);
                waresConfigParamList.add(waresConfigParam);
            }

            WaresRuleConfigParam waresRuleConfig1 = BeanUtil.copyProperties(acWaresRuleConfig, WaresRuleConfigParam.class);
            waresRuleConfig1.setWaresGiftConfigList(giftConfigList);
            waresRuleConfig1.setOrderType(acWaresRuleConfig.getOrderType());
            waresRuleConfigParam.add(waresRuleConfig1);
        }
        activityParam.setWaresConfigParam(waresConfigParamList);

        LambdaQueryWrapper<AcGoalMemberConfig> qr = new LambdaQueryWrapper<>();
        qr.eq(AcGoalMemberConfig::getPkBaseId, pkId);
        List<AcGoalMemberConfig> goalMemberConfigList = goalMemberConfigService.list(qr);
        activityParam.setGoalMemberConfigList(goalMemberConfigList);
        activityParam.setPkBaseId(baseConfig.getPkId());
        return AjaxResult.success(activityParam);
    }

    /**
     * 删除
     *
     * @param pkId
     * @return
     */
    @DeleteMapping("/{pkId}")
    public AjaxResult remove(@PathVariable Long pkId) {
        waresRuleConfigService.removeWaresRuleConfig(pkId);
        return AjaxResult.success();
    }

    /**
     * 修改活动信息
     *
     * @return
     */
    @PostMapping("update")
    public AjaxResult update(@RequestBody ActivityParam activityParam) {
        activityParam.setPkCountry(SecurityUtils.getPkCountry());
        activityParam.setPkCreator(SecurityUtils.getUserId());
        activityParam.setActType(EActType.PRODUCT_GIFT.getValue());
        waresRuleConfigService.updateWaresRuleConfig(activityParam);
        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.AC_WARES_RULE_CONFIG, business = EOperationBusiness.AC_WARES_RULE_CONFIG, 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());
        waresRuleConfigService.onLinePetition(recommendParam);
        return AjaxResult.success();
    }

}
