package com.hzs.system.config.controller.manage;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.enums.ESpecialArea;
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.member.base.CuMember;
import com.hzs.common.domain.system.config.BdMemberSpecialCurrency;
import com.hzs.common.domain.system.config.ext.BdMemberSpecialCurrencyExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.member.base.IMemberServiceApi;
import com.hzs.system.config.param.BdMemberSpecialCurrencyParam;
import com.hzs.system.config.param.BdMemberSpecialCurrencySaveParam;
import com.hzs.system.config.service.IBdMemberSpecialCurrencyService;
import com.hzs.system.config.vo.BdMemberSpecialCurrencyVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 会员特殊支付配置
 * @Author: jiang chao
 * @Time: 2025/2/20 16:39
 * @Classname: BdMemberSpecialCurrencyController
 * @PackageName: com.hzs.system.config.controller.manage
 */
@RestController
@RequestMapping("/manage/member-special-currency")
public class BdMemberSpecialCurrencyController extends BaseController {

    @Autowired
    private IBdMemberSpecialCurrencyService iBdMemberSpecialCurrencyService;

    @DubboReference
    IMemberServiceApi iMemberServiceApi;

    /**
     * 查询配置列表
     *
     * @param param
     * @return
     */
    @Log(module = EOperationModule.CURRENCY_CONFIG, business = EOperationBusiness.MEMBER_SPECIAL_CURRENCY, method = EOperationMethod.SELECT)
    @GetMapping("/config-list")
    public TableDataInfo configList(BdMemberSpecialCurrencyParam param) {
        param.setPkCountry(SecurityUtils.getPkCountry());
        param.setSystemType(SecurityUtils.getSystemType());

        List<BdMemberSpecialCurrencyVO> resultList = new ArrayList<>();
        // 查询配置数据列表
        startPage();
        List<BdMemberSpecialCurrencyExt> list = iBdMemberSpecialCurrencyService.listData(param);
        if (CollectionUtil.isNotEmpty(list)) {
            for (BdMemberSpecialCurrencyExt memberSpecialCurrency : list) {
                BdMemberSpecialCurrencyVO vo = BeanUtil.copyProperties(memberSpecialCurrency, BdMemberSpecialCurrencyVO.class);
                vo.setSpecialAreaVal(ESpecialArea.getESpecialArea(vo.getSpecialArea()));
                resultList.add(vo);
            }
        }

        TableDataInfo tableDataInfo = getDataTable(list);
        tableDataInfo.setRows(resultList);
        return tableDataInfo;
    }

    /**
     * 保存配置
     *
     * @param param
     * @return
     */
    @Log(module = EOperationModule.CURRENCY_CONFIG, business = EOperationBusiness.MEMBER_SPECIAL_CURRENCY, method = EOperationMethod.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@Valid @RequestBody BdMemberSpecialCurrencySaveParam param) {
        // 校验会员编号是否存在
        CuMember cuMember = iMemberServiceApi.getRetailMember(null, param.getMemberCode()).getData();
        if (null == cuMember) {
            return AjaxResult.error("会员编号不存在");
        }

        // 校验会员对应专区是否已经配置
        if (iBdMemberSpecialCurrencyService.count(Wrappers.<BdMemberSpecialCurrency>lambdaQuery()
                .eq(BdMemberSpecialCurrency::getPkMember, cuMember.getPkId())
                .eq(BdMemberSpecialCurrency::getSpecialArea, param.getSpecialArea())
                .ne(null != param.getPkId(), BdMemberSpecialCurrency::getPkId, param.getPkId())
        ) != 0) {
            return AjaxResult.error("会员专区配置已经存在");
        }

        param.setPkMember(cuMember.getPkId());
        param.setPkCountry(SecurityUtils.getPkCountry());
        param.setSystemType(SecurityUtils.getSystemType());
        iBdMemberSpecialCurrencyService.saveData(param, SecurityUtils.getUserId());
        return AjaxResult.success();
    }

    /**
     * 查看详情配置
     *
     * @param pkId
     * @return
     */
    @GetMapping("/detail")
    public AjaxResult detail(@RequestParam Integer pkId) {
        // 查询配置数据
        BdMemberSpecialCurrencyExt memberSpecialCurrency = iBdMemberSpecialCurrencyService.getData(pkId);
        BdMemberSpecialCurrencyVO vo = BeanUtil.copyProperties(memberSpecialCurrency, BdMemberSpecialCurrencyVO.class);
        vo.setSpecialAreaVal(ESpecialArea.getESpecialArea(vo.getSpecialArea()));
        return AjaxResult.success(vo);
    }

    /**
     * 删除配置
     *
     * @param pkId
     * @return
     */
    @Log(module = EOperationModule.CURRENCY_CONFIG, business = EOperationBusiness.MEMBER_SPECIAL_CURRENCY, method = EOperationMethod.DELETE)
    @DeleteMapping("/delete")
    public AjaxResult delete(@RequestParam Integer pkId) {
        iBdMemberSpecialCurrencyService.deleteData(pkId, SecurityUtils.getUserId());
        return AjaxResult.success();
    }

}
