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


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.msg.ConfigMsgConstants;
import com.hzs.common.core.domain.R;
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.attest.CuAuthorityControl;
import com.hzs.common.domain.member.attest.ext.CuAuthorityControlExt;
import com.hzs.common.domain.member.ext.CuMemberExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.util.TransactionUtils;
import com.hzs.member.base.IMemberServiceApi;
import com.hzs.system.config.service.ICuAuthorityControlService;
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.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 会员显示权限控制 前端控制器
 * </p>
 *
 * @author zhangjing
 * @since 2024-06-21
 */
@RestController
@RequestMapping("/manage/cu-authority-control")
public class CuAuthorityControlController extends BaseController {
    @Autowired
    private ICuAuthorityControlService iCuAuthorityControlService;
    @DubboReference
    private IMemberServiceApi iMemberServiceApi;

    /**
     * @description: 会员控制配置列表
     * @author: zhang jing
     * @date: 2024/6/21 10:51
     * @param: [cuAuthorityControl]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @Log(module = EOperationModule.AUTHORITY_CONTROL,business = EOperationBusiness.AUTHORITY_CONTROL, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(CuAuthorityControlExt controlExt) {
        startPage();
        controlExt.setPkCountry(SecurityUtils.getPkCountry());
        List<CuAuthorityControlExt> list = iCuAuthorityControlService.queryAuthorityControlList(controlExt);
        if(CollectionUtil.isNotEmpty(list)){
            list.forEach(itm->{
                itm.setControlTypeVal(EAuthorityControlType.getAuthorityControlType(itm.getControlType()));
            });
        }
        return getDataTable(list);
    }

    /**
     * @description: 导出会员控制配置
     * @author: zhang jing
     * @date: 2023/3/9 16:08
     * @param: [response, cuAuthorityControl]
     * @return: void
     **/
    @Log(module = EOperationModule.AUTHORITY_CONTROL,business = EOperationBusiness.AUTHORITY_CONTROL, method = EOperationMethod.SELECT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CuAuthorityControlExt controlExt) {
        controlExt.setPkCountry(SecurityUtils.getPkCountry());
        List<CuAuthorityControlExt> list = iCuAuthorityControlService.queryAuthorityControlList(controlExt);
        if(CollectionUtil.isNotEmpty(list)){
            list.forEach(itm->{
                itm.setControlTypeVal(EAuthorityControlType.getAuthorityControlType(itm.getControlType()));
            });
        }
        ExcelUtil<CuAuthorityControlExt> util = new ExcelUtil<>(CuAuthorityControlExt.class);
        util.exportExcel(response, list, "会员控制配置");
    }

    /**
     * @description: 新增会员控制配置
     * @author: zhang jing
     * @date: 2024/6/21 10:51
     * @param: [cuAuthorityControl]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.AUTHORITY_CONTROL,business = EOperationBusiness.AUTHORITY_CONTROL, method = EOperationMethod.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody CuAuthorityControlExt controlExt) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        if (StringUtils.isEmpty(controlExt.getMemberCode())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_CODE_NOT_NULL));
        }
        R<CuMemberExt> member = iMemberServiceApi.getMemberByCode(controlExt.getMemberCode());
        if (StringUtils.isNull(member.getData())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_NOT_VALID));
        }
        QueryWrapper<CuAuthorityControl> queryWrapper = new QueryWrapper();
        queryWrapper.eq("PK_COUNTRY", pkCountry);
        queryWrapper.eq("CONTROL_TYPE", controlExt.getControlType());
        queryWrapper.eq("PK_MEMBER",member.getData().getPkId());
        List<CuAuthorityControl> list = iCuAuthorityControlService.list(queryWrapper);
        if (CollectionUtil.isNotEmpty(list)) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_AUTHORITY_CONTROL));
        }
        CuAuthorityControl cac= new CuAuthorityControl();
        cac.setPkMember(member.getData().getPkId());
        cac.setControlType(controlExt.getControlType());
        cac.setCreationTime(new Date());
        cac.setPkCreator(SecurityUtils.getUserId());
        cac.setPkCountry(pkCountry);
        return AjaxResult.success(iCuAuthorityControlService.save(cac));
    }



    /**
     * @description:修改会员控制配置
     * @author: zhang jing
     * @date: 2024/6/21 10:51
     * @param: [cuAuthorityControl]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.AUTHORITY_CONTROL,business = EOperationBusiness.AUTHORITY_CONTROL, method = EOperationMethod.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody CuAuthorityControlExt controlExt) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        if (StringUtils.isEmpty(controlExt.getMemberCode())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_CODE_NOT_NULL));
        }
        R<CuMemberExt> member = iMemberServiceApi.getMemberByCode(controlExt.getMemberCode());
        if (StringUtils.isNull(member.getData().getPkId())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_NOT_VALID));
        }
        QueryWrapper<CuAuthorityControl> queryWrapper = new QueryWrapper();
        queryWrapper.ne("PK_ID",controlExt.getPkId());
        queryWrapper.eq("PK_COUNTRY", pkCountry);
        queryWrapper.eq("PK_MEMBER",member.getData().getPkId());
        queryWrapper.eq("CONTROL_TYPE", controlExt.getControlType());
        List<CuAuthorityControl> list = iCuAuthorityControlService.list(queryWrapper);
        if (CollectionUtil.isNotEmpty(list)) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_AUTHORITY_CONTROL));
        }
        CuAuthorityControl cac= new CuAuthorityControl();
        cac.setPkMember(member.getData().getPkId());
        cac.setModifiedTime(new Date());
        cac.setPkId(controlExt.getPkId());
        cac.setPkModified(SecurityUtils.getUserId());
        cac.setControlType(controlExt.getControlType());
        return toAjax(iCuAuthorityControlService.updateById(cac));
    }

    /**
     * @description: 修改会员控制配置
     * @author: zhang jing
     * @date: 2024/6/21 10:51
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.AUTHORITY_CONTROL,business = EOperationBusiness.AUTHORITY_CONTROL, method = EOperationMethod.SELECT)
    @GetMapping("/getOne/{pkId}")
    public AjaxResult getOne(@PathVariable Long pkId) {
        CuAuthorityControlExt cacExt=new CuAuthorityControlExt();
        CuAuthorityControl cuAuthorityControl = iCuAuthorityControlService.getById(pkId);
        if(StringUtils.isNotNull(cuAuthorityControl)){
            R<CuMemberExt> member = iMemberServiceApi.getMemberById(cuAuthorityControl.getPkMember());
            cacExt.setPkId(pkId);
            cacExt.setMemberCode(member.getData().getMemberCode());
            cacExt.setMemberName(member.getData().getMemberName());
            cacExt.setControlType(cuAuthorityControl.getControlType());
        }
        return AjaxResult.success(cacExt);
    }


    /**
     * @description: 删除会员控制配置
     * @author: zhang jing
     * @date: 2024/6/21 10:52
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.AUTHORITY_CONTROL,business = EOperationBusiness.AUTHORITY_CONTROL, method = EOperationMethod.DELETE)
    @PostMapping("/delControl")
    public AjaxResult delete(@RequestBody CuAuthorityControlExt controlExt) {
        if(StringUtils.isNotNull(controlExt.getPkIdStr())){
            List<Long> pkIdList = Arrays.stream(controlExt.getPkIdStr().split(","))
                    .map(Long::parseLong)
                    .collect(Collectors.toList());
            return toAjax(iCuAuthorityControlService.updateControl(pkIdList));
        }
        return toAjax(true);
    }

}
