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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzs.common.core.annotation.AccessPermissions;
import com.hzs.common.core.constant.EnumsPrefixConstants;
import com.hzs.common.core.constant.msg.CommonMsgConstants;
import com.hzs.common.core.constant.msg.MemberMsgConstants;
import com.hzs.common.core.utils.DateUtils;
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.base.CuMember;
import com.hzs.common.domain.member.ext.CuMemberExt;
import com.hzs.common.domain.member.swing.CuMemberSwingNet;
import com.hzs.common.domain.member.swing.ext.CuMemberSwingNetLogExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.service.ITransactionCommonService;
import com.hzs.common.util.TransactionUtils;
import com.hzs.member.base.service.ICuMemberService;
import com.hzs.member.swing.param.CuMemberSwingNetParam;
import com.hzs.member.swing.param.SwingNetLogParam;
import com.hzs.member.swing.service.ICuMemberSwingNetService;
import com.hzs.member.swing.vo.CuMemberRetVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 会员网体甩网配置 前端控制器
 */
@RestController
@RequestMapping("/manage/swing")
public class CuMemberSwingNetController extends BaseController {

    @Autowired
    private ICuMemberSwingNetService cuMemberSwingNetService;
    @Autowired
    private ICuMemberService cuMemberService;
    @Autowired
    private ITransactionCommonService iTransactionCommonService;

    /*
     *  查询甩网数据列表
     **/
    @AccessPermissions("AccessCode")
    @PostMapping("/query-net")
    public TableDataInfo queryCuMemberSwingNet(@RequestBody CuMemberSwingNetParam param) {
        param.setPkCountry(SecurityUtils.getPkCountry());

        if (null != param.getStartCreationTime()) {
            param.setStartCreationTime(DateUtils.getStartTime(param.getStartCreationTime()));
        }
        if (null != param.getEndCreationTime()) {
            param.setEndCreationTime(DateUtils.getEndTime(param.getEndCreationTime()));
        }

        startPage();
        return getDataTable(cuMemberSwingNetService.queryCuMemberSwingNetExt(param));
    }

    /*
     *  保存甩网配置
     **/
    @PutMapping("/save-net")
    public AjaxResult saveCuMemberSwingNet(@RequestBody CuMemberSwingNetParam param) {
        // 校验数据是否为空
        if (null == param.getPkSourceMember() || null == param.getPkTargetMember() || CollectionUtil.isEmpty(param.getCuMemberSwingNetDetailExtList())) {
            // 缺少参数
            return AjaxResult.error(TransactionUtils.getContent(CommonMsgConstants.MISSING_PARAM));
        }

        // 校验开始节点、接入节点不能出现重复
        QueryWrapper<CuMemberSwingNet> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PK_SOURCE_MEMBER", param.getPkSourceMember());
        queryWrapper.eq("PK_TARGET_MEMBER", param.getPkTargetMember());
        List<CuMemberSwingNet> list = cuMemberSwingNetService.list(queryWrapper);
        if (CollectionUtil.isNotEmpty(list)) {
            if (null == param.getPkId()) {
                return AjaxResult.error("开始编号、接入编号甩网配置已存在");
            } else {
                if (list.stream().anyMatch((net) -> !net.getPkId().equals(param.getPkId()))) {
                    return AjaxResult.error("开始编号、接入编号甩网配置已存在");
                }
            }
        }

        cuMemberSwingNetService.saveCuMemberSingNet(param, SecurityUtils.getPkCountry(), SecurityUtils.getUserId());
        return AjaxResult.success();
    }

    /**
     * 查询会员信息
     *
     * @param param
     * @return
     */
    @PostMapping("/query-member")
    public AjaxResult queryMember(@RequestBody CuMemberSwingNetParam param) {
        if (StringUtils.isEmpty(param.getMemberCode())) {
            return AjaxResult.error(TransactionUtils.getContent(MemberMsgConstants.MEMBER_NOT_EXITS));
        }
        CuMember cuMember = cuMemberService.getMember(param.getMemberCode());
        if (cuMember == null) {
            return AjaxResult.error(TransactionUtils.getContent(MemberMsgConstants.MEMBER_NOT_EXITS));
        }
        return AjaxResult.success(BeanUtil.copyProperties(cuMember, CuMemberRetVO.class));
    }

    /**
     * 根据会员编号查询伞上所有人
     *
     * @param param
     * @return
     */
    @PostMapping("/query-member-parent")
    public AjaxResult queryMemberParent(@RequestBody CuMemberSwingNetParam param) {
        if (StringUtils.isEmpty(param.getMemberCode())) {
            return AjaxResult.error(TransactionUtils.getContent(MemberMsgConstants.MEMBER_NOT_EXITS));
        }
        CuMember cuMember = cuMemberService.getMember(param.getMemberCode());
        if (cuMember == null) {
            return AjaxResult.error(TransactionUtils.getContent(MemberMsgConstants.MEMBER_NOT_EXITS));
        }
        List<CuMemberExt> cuMemberExtList = cuMemberService.queryPlaceSponsorOnUmbrella(cuMember.getPkId(), null, null);
        return AjaxResult.success(cuMemberExtList);
    }

    /**
     * 删除甩网数据
     *
     * @param pkId
     * @return
     */
    @DeleteMapping("/delete-net/{pkId}")
    public AjaxResult deleteNet(@PathVariable("pkId") Long pkId) {
        cuMemberSwingNetService.deleteNet(pkId, SecurityUtils.getUserId());
        return AjaxResult.success();
    }


    /**
     * 甩网配置详情
     **/
    @GetMapping("/swingNetLog")
    public TableDataInfo swingNetLogList(SwingNetLogParam param) {
        param.setPkCountry(SecurityUtils.getPkCountry());

        if (null != param.getStartDate()) {
            param.setStartDate(DateUtils.getStartTime(param.getStartDate()));
        }
        if (null != param.getEndDate()) {
            param.setEndDate(DateUtils.getEndTime(param.getEndDate()));
        }

        startPage();
        return getDataTable(cuMemberSwingNetService.swingNetLogList(param));
    }

    /**
     * 导出甩网配置详情
     **/
    @PostMapping("/export-swingNetLog")
    public void exportSwingNetLog(HttpServletResponse response, SwingNetLogParam param) {
        param.setPkCountry(SecurityUtils.getPkCountry());

        if (null != param.getStartDate()) {
            param.setStartDate(DateUtils.getStartTime(param.getStartDate()));
        }
        if (null != param.getEndDate()) {
            param.setEndDate(DateUtils.getEndTime(param.getEndDate()));
        }
        List<CuMemberSwingNetLogExt> list = cuMemberSwingNetService.swingNetLogList(param);
        if (CollectionUtil.isNotEmpty(list)) {
            Map<Integer, String> transactionMap1 = iTransactionCommonService.exportEnumTransactionByDB(
                    EnumsPrefixConstants.KEY_AWARD);
            for (CuMemberSwingNetLogExt csn : list) {
                csn.setPkAwardsVal(transactionMap1.get(csn.getPkAwards()));
            }
        }
        ExcelUtil<CuMemberSwingNetLogExt> util = new ExcelUtil<>(CuMemberSwingNetLogExt.class);
        util.exportExcel(response, list, "甩网配置详情");
    }

}
