package com.hzs.member.base.controller.manage;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.msg.ConfigMsgConstants;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.utils.StringUtils;
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.core.web.page.TableDataInfo;
import com.hzs.common.domain.member.ext.CuMemberRegionExt;
import com.hzs.common.domain.member.region.CuMemberRegion;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.member.base.param.MemberRegionParam;
import com.hzs.member.base.service.ICuMemberRegionService;
import com.hzs.member.base.service.ICuMemberService;
import com.hzs.member.base.vo.UnboundAreaVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * @description: 新零售收益区域配置
 * @author: zhang jing
 * @date: 2024/12/25 12:03
 * @param:
 * @return:
 **/
@RestController
@RequestMapping("/manage/cu-member-region")
public class CuMemberRegionController extends BaseController {

    @Autowired
    private ICuMemberRegionService CuMemberRegionService;
    @Autowired
    private ICuMemberService iCuMemberService;


    /**
     * @description: 会员收益区域配置
     * @author: zhang jing
     * @date: 2024/12/25 15:02
     * @param: [mrrExt]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @Log(module = EOperationModule.CU_MEMBER_RETAIL_REGION, business = EOperationBusiness.CU_MEMBER_RETAIL_REGION, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(CuMemberRegionExt mrrExt) {
        mrrExt.setPkCountry(SecurityUtils.getPkCountry());
        mrrExt.setSystemType(SecurityUtils.getSystemType());

        startPage();
        List<CuMemberRegionExt> list = CuMemberRegionService.memberretailRegList(mrrExt);
        if (CollectionUtil.isNotEmpty(list)) {
            list.forEach(itm -> {
                itm.setSourceVal(ENoticeWebsiteType.getNoticeWebsiteType(itm.getSource()).getLabel());
            });
        }
        return getDataTable(list);
    }

    /**
     * @description: 导出会员收益区域配置
     * @author: zhang jing
     * @date: 2024/12/25 14:59
     * @param: [response, mrrExt]
     * @return: void
     **/
    @Log(module = EOperationModule.CU_MEMBER_RETAIL_REGION, business = EOperationBusiness.CU_MEMBER_RETAIL_REGION, method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CuMemberRegionExt mrrExt) {
        mrrExt.setPkCountry(SecurityUtils.getPkCountry());
        mrrExt.setSystemType(SecurityUtils.getSystemType());

        List<CuMemberRegionExt> list = CuMemberRegionService.memberretailRegList(mrrExt);
        if (CollectionUtil.isNotEmpty(list)) {
            list.forEach(itm -> {
                itm.setSourceVal(ENoticeWebsiteType.getNoticeWebsiteType(itm.getSource()).getLabel());
            });
        }
        ExcelUtil<CuMemberRegionExt> util = new ExcelUtil<>(CuMemberRegionExt.class);
        util.exportExcel(response, list, "会员收益区域配置");
    }

    /**
     * @description: 未绑定收益区域
     * @author: zhang jing
     * @date: 2025/1/9 13:54
     * @param: []
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @GetMapping("/regionAreaList")
    public TableDataInfo regionAreaList() {
        startPage();
        List<UnboundAreaVO> areaList = CuMemberRegionService.unboundAreaList(SecurityUtils.getPkCountry(), SecurityUtils.getSystemType());
        return getDataTable(areaList);
    }

    /**
     * @description: 导出未配置收益区域
     * @author: zhang jing
     * @date: 2025/1/15 11:05
     * @param: [response, mrrExt]
     * @return: void
     **/
    @Log(module = EOperationModule.NOT_USED_REGION_AREA, business = EOperationBusiness.NOT_USED_REGION_AREA, method = EOperationMethod.EXPORT)
    @PostMapping("/export-region-area")
    public void exportRegionAreaList(HttpServletResponse response) {
        List<UnboundAreaVO> areaList = CuMemberRegionService.unboundAreaList(SecurityUtils.getPkCountry(), SecurityUtils.getSystemType());
        ExcelUtil<UnboundAreaVO> util = new ExcelUtil<>(UnboundAreaVO.class);
        util.exportExcel(response, areaList, "未配置收益区域");
    }

    /**
     * @description:新增会员收益区域
     * @author: zhang jing
     * @date: 2024/12/25 16:18
     * @param: [mrrExt]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.CU_MEMBER_RETAIL_REGION, business = EOperationBusiness.CU_MEMBER_RETAIL_REGION, method = EOperationMethod.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody @Valid MemberRegionParam mrrExt) {
        return iCuMemberService.setRegionForBack(mrrExt);
    }


    /**
     * @description: 修改会员收益区域
     * @author: zhang jing
     * @date: 2024/12/25 16:18
     * @param: [mrrExt]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.CU_MEMBER_RETAIL_REGION, business = EOperationBusiness.CU_MEMBER_RETAIL_REGION, method = EOperationMethod.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody CuMemberRegionExt mrrExt) {
        if (StringUtils.isNull(mrrExt.getPkMember())) {
            return AjaxResult.error(ConfigMsgConstants.MEMBER_NOT_NULL);
        }
        if (StringUtils.isNull(mrrExt.getProvince())) {
            return AjaxResult.error(ConfigMsgConstants.GLO_PROVINCE_NOT_NULL);
        }
        if (StringUtils.isNull(mrrExt.getCity())) {
            return AjaxResult.error(ConfigMsgConstants.GLO_CITY_NOT_NULL);
        }

        // 校验区域是否被占用
//        int CuMemberRegion = CuMemberRegionService.count(Wrappers.<CuMemberRegion>lambdaQuery()
//                .eq(CuMemberRegion::getProvince, mrrExt.getProvince())
//                .eq(CuMemberRegion::getCity, mrrExt.getCity())
//                .eq(CuMemberRegion::getCounty, mrrExt.getCounty())
//                .eq(CuMemberRegion::getEffective, EYesNo.YES.getIntValue())
//                .eq(CuMemberRegion::getSystemType, SecurityUtils.getSystemType())
//                .ne(CuMemberRegion::getPkId, mrrExt.getPkId())
//        );
        int CuMemberRegion = CuMemberRegionService.validateBindCount(mrrExt, EYesNo.YES.getIntValue(), SecurityUtils.getSystemType(), mrrExt.getPkMember());
        if (CuMemberRegion > 0) {
            return AjaxResult.error("当前区域已经被占用");
        }
        mrrExt.setPkModified(SecurityUtils.getUserId());
        mrrExt.setModifiedTime(new Date());
        mrrExt.setRegionVertexPkId(CuMemberRegionService.getRegionVertexPkIdByPkMember(mrrExt.getPkMember()));
        return toAjax(CuMemberRegionService.updateById(mrrExt));
    }


    /**
     * @description: 查询会员收益区域
     * @author: zhang jing
     * @date: 2024/12/25 16:18
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.CU_MEMBER_RETAIL_REGION, business = EOperationBusiness.CU_MEMBER_RETAIL_REGION, method = EOperationMethod.SELECT)
    @GetMapping("/getOne/{pkId}")
    public AjaxResult getOne(@PathVariable Integer pkId) {
        CuMemberRegion withdrawalProcess = CuMemberRegionService.getById(pkId);
        return AjaxResult.success(withdrawalProcess);
    }


    /**
     * @description: 删除会员收益区域
     * @author: zhang jing
     * @date: 2024/12/25 16:18
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.CU_MEMBER_RETAIL_REGION, business = EOperationBusiness.CU_MEMBER_RETAIL_REGION, method = EOperationMethod.DELETE)
    @DeleteMapping("/{pkId}")
    public AjaxResult delete(@PathVariable Long pkId) {
        LambdaUpdateWrapper<CuMemberRegion> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(CuMemberRegion::getPkId, pkId);
        updateWrapper.set(CuMemberRegion::getDelFlag, EDelFlag.DELETE.getValue());
        updateWrapper.set(CuMemberRegion::getPkModified, SecurityUtils.getUserId());
        updateWrapper.set(CuMemberRegion::getModifiedTime, new Date());
        return toAjax(CuMemberRegionService.update(updateWrapper));
    }

}
