package com.hzs.activity.pick.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.service.IAcBaseConfigService;
import com.hzs.activity.pick.service.IAcRetailPickProductConfigService;
import com.hzs.activity.pick.vo.AcRetailPickProductConfigVO;
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.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.pick.AcRetailPickProductConfig;
import com.hzs.common.domain.activity.pick.ext.AcRetailPickProductConfigExt;
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 java.util.ArrayList;
import java.util.List;

/**
 * @description: 会员提货赠送活动配置
 * @author: zhang jing
 * @date: 2024/11/28 11:28
 * @param:
 * @return:
 **/
@RestController
@RequestMapping("/manage/pick-product-config")
public class AcRetailPickProductConfigController extends BaseController {

    @Autowired
    private UserTokenService userTokenService;
    @Autowired
    private IAcRetailPickProductConfigService iAcRetailPickProductConfigService;
    @Autowired
    private IAcBaseConfigService baseConfigService;


    /**
     * 查询详情
     *
     * @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<AcRetailPickProductConfig> qw = new LambdaQueryWrapper<>();
            qw.eq(AcRetailPickProductConfig::getPkBaseId, baseConfig.getPkId());
            List<AcRetailPickProductConfig> consumeRuleConfigList = iAcRetailPickProductConfigService.list(qw);
            List<AcRetailPickProductConfigExt> mcrList = new ArrayList<>();
            for (AcRetailPickProductConfig acMemberConsumeRule : consumeRuleConfigList) {
                AcRetailPickProductConfigExt cv = BeanUtil.copyProperties(acMemberConsumeRule, AcRetailPickProductConfigExt.class);
                mcrList.add(cv);
            }
            activityParam.setRppcList(mcrList);
            return AjaxResult.success(activityParam);
        } else {
            return AjaxResult.success();
        }
    }


    /**
     * 在线签呈
     *
     * @param rppcVo 提货赠送活动参数
     * @return AjaxResult
     */
    @Log(module = EOperationModule.RETAIL_PICK_PRODUCT_GIVE, business = EOperationBusiness.RETAIL_PICK_PRODUCT_GIVE, method = EOperationMethod.INSERT, remark = "在线签呈")
    @PostMapping("/online-petition")
    public AjaxResult onLinePetition(@RequestBody AcRetailPickProductConfigVO rppcVo) {
        rppcVo.setLoginUser(userTokenService.getLoginUser());
        rppcVo.setSystemType(SecurityUtils.getSystemType());
        iAcRetailPickProductConfigService.onLinePetition(rppcVo);
        return AjaxResult.success();
    }

}
