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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.hzs.common.core.annotation.AccessPermissions;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.EnumsPrefixConstants;
import com.hzs.common.core.constant.msg.FinanceMsgConstants;
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.account.CuMemberBusinessLicenseExt;
import com.hzs.common.domain.system.config.BdAwards;
import com.hzs.common.security.service.UserTokenService;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.service.ITransactionCommonService;
import com.hzs.common.util.TransactionUtils;
import com.hzs.member.account.service.ICuMemberBusinessLicenseService;
import com.hzs.member.account.vo.CuMemberBusinessLicenseVO;
import com.hzs.system.config.IAwardsServiceApi;
import com.hzs.system.sys.dto.LoginUser;
import org.apache.dubbo.config.annotation.DubboReference;
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;
import java.util.Map;

/**
 * <p>
 * 会员营业执照 前端控制器
 * </p>
 *
 * @author hzs
 * @since 2023-08-17
 */
@RestController
@RequestMapping("/manager/business/license")
public class CuMemberBusinessLicenseController extends BaseController {

    @Autowired
    ICuMemberBusinessLicenseService cuMemberBusinessLicenseService;

    @Autowired
    ITransactionCommonService iTransactionCommonService;

    @Autowired
    private UserTokenService userTokenService;

    @DubboReference
    IAwardsServiceApi iAwardsServiceApi;

    /**
     * 会员营业执照查询
     *
     * @param licenseVO 入参
     * @return TableDataInfo
     */
    @AccessPermissions("BusinessLicense")
    @Log(module = EOperationModule.BUSINESS_LICENSE, business = EOperationBusiness.BUSINESS_LICENSE_LIST, method = EOperationMethod.SELECT)
    @PostMapping("/list")
    public TableDataInfo findLicenseList(@RequestBody CuMemberBusinessLicenseVO licenseVO) {
        licenseVO.setPkCountry(SecurityUtils.getPkCountry());

        List<Integer> queryAwardsList = new ArrayList<>();
        // 查询总监级奖衔ID
        List<BdAwards> awardsList = iAwardsServiceApi.queryAwards(SecurityUtils.getPkCountry()).getData();
        if (CollectionUtil.isNotEmpty(awardsList)) {
            for (BdAwards bdAwards : awardsList) {
                if (bdAwards.getAwardsValue().equals(EAwards.CHIEF.getValue())
                        || bdAwards.getAwardsValue().equals(EAwards.HIGH_CHIEF.getValue())
                        || bdAwards.getAwardsValue().equals(EAwards.SENIOR_CHIEF.getValue())) {
                    queryAwardsList.add(bdAwards.getPkId());
                }
            }
        }
        licenseVO.setAwardsList(queryAwardsList);

        startPage();
        Object[] objects = cuMemberBusinessLicenseService.selectLicenseList(licenseVO);
        TableDataInfo tableDataInfo = getDataTable((List<CuMemberBusinessLicenseExt>) objects[0]);
        tableDataInfo.setRows((List<CuMemberBusinessLicenseVO>) objects[1]);
        return tableDataInfo;
    }


    /**
     * 会员营业执照列表导出
     *
     * @param response  响应
     * @param licenseVO 查询入参
     */
    @Log(module = EOperationModule.BUSINESS_LICENSE, business = EOperationBusiness.BUSINESS_LICENSE_LIST, method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CuMemberBusinessLicenseVO licenseVO) {
        licenseVO.setPkCountry(SecurityUtils.getPkCountry());

        List<Integer> queryAwardsList = new ArrayList<>();
        // 查询总监级奖衔ID
        List<BdAwards> awardsList = iAwardsServiceApi.queryAwards(SecurityUtils.getPkCountry()).getData();
        if (CollectionUtil.isNotEmpty(awardsList)) {
            for (BdAwards bdAwards : awardsList) {
                if (bdAwards.getAwardsValue().equals(EAwards.CHIEF.getValue())
                        || bdAwards.getAwardsValue().equals(EAwards.HIGH_CHIEF.getValue())
                        || bdAwards.getAwardsValue().equals(EAwards.SENIOR_CHIEF.getValue())) {
                    queryAwardsList.add(bdAwards.getPkId());
                }
            }
        }
        licenseVO.setAwardsList(queryAwardsList);

        List<CuMemberBusinessLicenseVO> licenseVOList = (List<CuMemberBusinessLicenseVO>) cuMemberBusinessLicenseService.selectLicenseList(licenseVO)[1];
        //多语言翻译
        if (CollUtil.isNotEmpty(licenseVOList)) {
            //枚举翻译
            Map<String, String> transactionMap = iTransactionCommonService.exportEnumTransaction(EApproveStatus.values());
            Map<Integer, String> transactionDBMap = iTransactionCommonService.exportEnumTransactionByDB(EnumsPrefixConstants.KEY_AWARD);
            for (CuMemberBusinessLicenseVO memberBusinessLicenseVO : licenseVOList) {
                memberBusinessLicenseVO.setApproveStatusVal(transactionMap.get(EnumsPrefixConstants.APPROVE_STATUS + memberBusinessLicenseVO.getApproveStatus()));
                memberBusinessLicenseVO.setAwardTransactionVal(transactionDBMap.get(memberBusinessLicenseVO.getAwardTransaction()));
            }
        }
        ExcelUtil<CuMemberBusinessLicenseVO> util = new ExcelUtil<>(CuMemberBusinessLicenseVO.class);
        util.exportExcel(response, licenseVOList, TransactionUtils.getContent(FinanceMsgConstants.EXPORT_BUSINESS_LICENSE));
    }

    /**
     * 驳回
     *
     * @return AjaxResult
     */
    @PostMapping("/reject")
    @Log(module = EOperationModule.BUSINESS_LICENSE, business = EOperationBusiness.BUSINESS_LICENSE_LIST, method = EOperationMethod.REJECT)
    public AjaxResult licenseReject(@RequestBody CuMemberBusinessLicenseVO cmb) {
        if (StringUtils.isEmpty(cmb.getRejectMsg())) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.REJECT_MSG_NOT_NULL));
        }
        CuMemberBusinessLicenseVO license = cuMemberBusinessLicenseService.selectLicenseDetail(cmb.getPkId());
        if (!license.getApproveStatus().equals(EApproveStatus.ALREADY_SUBMIT.getValue())) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.BUSINESS_LICENSE_NOT_REJECT));
        }
        return AjaxResult.success(cuMemberBusinessLicenseService.rejectById(cmb, SecurityUtils.getUserId()));
    }


    /**
     * 同意
     *
     * @return AjaxResult
     */
    @PostMapping("/agree")
    @Log(module = EOperationModule.BUSINESS_LICENSE, business = EOperationBusiness.BUSINESS_LICENSE_LIST, method = EOperationMethod.AGREE)
    public AjaxResult licenseAgree(@RequestBody CuMemberBusinessLicenseVO licenseVO) {
        CuMemberBusinessLicenseVO license = cuMemberBusinessLicenseService.selectLicenseDetail(licenseVO.getPkId());
        if (null == license) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.BUSINESS_LICENSE_NOT_EXIST));
        }
        if (license.getApproveStatus().equals(EApproveStatus.APPROVE.getValue())) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.BUSINESS_LICENSE_IN_APPROVAL));
        }
        LoginUser loginUser = userTokenService.getLoginUser();
        licenseVO.setLoginUser(loginUser);
        licenseVO.setPkModified(SecurityUtils.getUserId());
        licenseVO.setPkCountry(SecurityUtils.getPkCountry());
        return AjaxResult.success(cuMemberBusinessLicenseService.licenseAgree(licenseVO, license));
    }

    /**
     * @description: 重置营业执照
     * @author: zhang jing
     * @date: 2023/11/20 10:35
     * @param: [licenseVO]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @PostMapping("/resetting")
    @Log(module = EOperationModule.BUSINESS_LICENSE, business = EOperationBusiness.BUSINESS_LICENSE_LIST, method = EOperationMethod.RESETTING)
    public AjaxResult resettingLicense(@RequestBody CuMemberBusinessLicenseVO licenseVO) {
        CuMemberBusinessLicenseVO license = cuMemberBusinessLicenseService.selectLicenseDetail(licenseVO.getPkId());
        if (null == license) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.BUSINESS_LICENSE_NOT_EXIST));
        }
        if (!license.getApproveStatus().equals(EApproveStatus.FINISH.getValue())) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.BUSINESS_LICENSE_REFUSE_RESET));
        }
        LoginUser loginUser = userTokenService.getLoginUser();
        licenseVO.setLoginUser(loginUser);
        licenseVO.setPkModified(SecurityUtils.getUserId());
        licenseVO.setPkCountry(SecurityUtils.getPkCountry());
        return AjaxResult.success(cuMemberBusinessLicenseService.resettingLicense(licenseVO, license));
    }


    /**
     * 详情
     *
     * @return AjaxResult
     */
    @GetMapping("/detail")
    @Log(module = EOperationModule.BUSINESS_LICENSE, business = EOperationBusiness.BUSINESS_LICENSE_LIST, method = EOperationMethod.SELECT)
    public AjaxResult licenseDetail(Long pkId) {
        return AjaxResult.success(cuMemberBusinessLicenseService.selectLicenseDetail(pkId));
    }


}
