package com.gic.haoban.manage.web.controller.hm;

import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.webapi.reponse.RestResponse;
import com.gic.haoban.base.api.common.pojo.dto.WebLoginDTO;
import com.gic.haoban.common.utils.AuthWebRequestUtil;
import com.gic.haoban.common.utils.StringUtil;
import com.gic.haoban.manage.api.dto.hm.HmGroupDTO;
import com.gic.haoban.manage.api.dto.hm.HmGroupQueryDTO;
import com.gic.haoban.manage.api.service.hm.HmGroupApiService;
import com.gic.haoban.manage.web.controller.WebBaseController;
import com.gic.haoban.manage.web.errCode.HaoBanErrCode;
import com.gic.haoban.manage.web.log.LogRecordUserServiceImpl;
import com.gic.haoban.manage.web.qo.hm.HmGroupQO;
import com.gic.haoban.manage.web.vo.hm.HmGroupSettingVO;
import com.gic.log.record.anno.GicLogRecord;
import com.gic.log.record.util.GicLogRecordCategoryEnum;
import com.gic.log.record.util.GicLogRecordEvaluationContext;
import com.gic.log.record.util.GicLogRecordOptTypeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * @Author MUSI
 * @Date 2022/7/1 4:31 PM
 * @Description
 * @Version
 **/
@RestController
@RequestMapping(path = "/hm/group")
public class HmGroupController extends WebBaseController {

    @Autowired
    private HmGroupApiService groupApiService;

    @GicLogRecord(value = "${#logContent}", category = GicLogRecordCategoryEnum.HUOMA, optType = GicLogRecordOptTypeEnum.HUOMA_GROUP_ADD
            ,optPage = "员工活码", userFunc = LogRecordUserServiceImpl.class )
    @RequestMapping(path = "/save", method = RequestMethod.POST)
    public RestResponse<?> saveHmGroupSetting(@Valid HmGroupSettingVO hmGroupSettingVO){
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        HmGroupDTO hmGroupDTO = new HmGroupDTO();
        hmGroupDTO.setGroupName(hmGroupSettingVO.getGroupName());
        hmGroupDTO.setCreatorId(loginUser.getHanbaoUserId());
        hmGroupDTO.setCreatorName(loginUser.getClerkName());
        hmGroupDTO.setEnterpriseId(loginUser.getEnterpriseId());
        hmGroupDTO.setWxEnterpriseId(loginUser.getWxEnterpriseId());
        ServiceResponse<Long> serviceResponse = groupApiService.saveOrUpdateHmGroupSetting(hmGroupDTO);
        if (serviceResponse.isSuccess()){
            String logContent = "新增分组【" + hmGroupDTO.getGroupName() + "】";
            GicLogRecordEvaluationContext.putAttribute("logContent", logContent);
            return RestResponse.successResult(serviceResponse.getResult());
        }
        GicLogRecordEvaluationContext.noWriteLog();
        return RestResponse.failure(serviceResponse.getCode(), serviceResponse.getMessage());
    }

    @RequestMapping(path = "/list")
    public RestResponse<?> queryHmGroupList(HmGroupQO hmGroupQo){
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        String wxEnterpriseId = loginUser.getWxEnterpriseId();
        String enterpriseId = loginUser.getEnterpriseId();
        HmGroupQueryDTO hmGroupQueryDTO = new HmGroupQueryDTO();
        hmGroupQueryDTO.setGroupName(hmGroupQo.getGroupName());
        hmGroupQueryDTO.setPageNum(hmGroupQo.getPageNum());
        hmGroupQueryDTO.setPageSize(hmGroupQo.getPageSize());
        hmGroupQueryDTO.setWxEnterpriseId(wxEnterpriseId);
        hmGroupQueryDTO.setEnterpriseId(enterpriseId);
        hmGroupQueryDTO.setClerkId(loginUser.getClerkId());
        ServiceResponse<Page<HmGroupDTO>> serviceResponse = groupApiService.queryHmGroupSettingList(hmGroupQueryDTO);
        return RestResponse.successResult(serviceResponse.getResult());
    }

    @GicLogRecord(value = "${#logContent}", category = GicLogRecordCategoryEnum.HUOMA, optType = GicLogRecordOptTypeEnum.HUOMA_GROUP_UPDATE
            ,optPage = "员工活码", userFunc = LogRecordUserServiceImpl.class )
    @RequestMapping(path = "/edit", method = RequestMethod.POST)
    public RestResponse<?> editHmGroupSetting(@Valid HmGroupSettingVO hmGroupSettingVO){
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        HmGroupDTO hmGroupDTO = new HmGroupDTO();
        hmGroupDTO.setGroupName(hmGroupSettingVO.getGroupName());
        hmGroupDTO.setModifierId(loginUser.getClerkId());
        hmGroupDTO.setModifierName(loginUser.getClerkName());
        hmGroupDTO.setGroupId(hmGroupSettingVO.getGroupId());
        hmGroupDTO.setEnterpriseId(loginUser.getEnterpriseId());
        hmGroupDTO.setWxEnterpriseId(loginUser.getWxEnterpriseId());

        ServiceResponse<HmGroupDTO> queryGroupDetail = groupApiService.queryGroupDetail(hmGroupDTO.getGroupId());

        ServiceResponse<Long> serviceResponse = groupApiService.saveOrUpdateHmGroupSetting(hmGroupDTO);
        if (serviceResponse.isSuccess()){
            String oldGroupName = Optional.ofNullable(queryGroupDetail.getResult()).map(HmGroupDTO::getGroupName).orElse("");
            String logContent = "修改分组【" + hmGroupDTO.getGroupName() + "】：分组名称「 " + oldGroupName + "」变更为「 " + hmGroupDTO.getGroupName() + "」";
            GicLogRecordEvaluationContext.putAttribute("logContent", logContent);
            return RestResponse.successResult(serviceResponse.getResult());
        }
        GicLogRecordEvaluationContext.noWriteLog();
        return RestResponse.failure(serviceResponse.getCode(), serviceResponse.getMessage());
    }

    @GicLogRecord(value = "${#logContent}", category = GicLogRecordCategoryEnum.HUOMA, optType = GicLogRecordOptTypeEnum.HUOMA_GROUP_DEL
            ,optPage = "员工活码", userFunc = LogRecordUserServiceImpl.class )
    @RequestMapping(path = "/delete", method = RequestMethod.POST)
    public RestResponse<?> deleteHmGroupSetting(HmGroupSettingVO hmGroupSettingVO){
        if (hmGroupSettingVO.getGroupId() == null){
            return RestResponse.failure(HaoBanErrCode.ERR_2.getCode()+"", HaoBanErrCode.ERR_2.getMsg());
        }
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        HmGroupDTO hmGroupDTO = new HmGroupDTO();
        hmGroupDTO.setModifierId(loginUser.getClerkId());
        hmGroupDTO.setModifierName(loginUser.getClerkName());
        hmGroupDTO.setGroupId(hmGroupSettingVO.getGroupId());

        ServiceResponse<HmGroupDTO> queryGroupDetail = groupApiService.queryGroupDetail(hmGroupDTO.getGroupId());
        ServiceResponse<Boolean> serviceResponse = groupApiService.deleteGroupSetting(hmGroupDTO);
        if (serviceResponse.isSuccess()){
            String logContent = "删除分组【" + queryGroupDetail.getResult().getGroupName()+ "】";
            GicLogRecordEvaluationContext.putAttribute("logContent", logContent);
            return RestResponse.successResult(serviceResponse.getResult());
        }
        GicLogRecordEvaluationContext.noWriteLog();
        return RestResponse.failure(serviceResponse.getCode(), serviceResponse.getMessage());
    }

    @RequestMapping(path = "/refer/count")
    public RestResponse<?> queryGroupTotalReferCount(){
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        ServiceResponse<Integer> serviceResponse = groupApiService.queryGroupTotalReferCount(loginUser.getClerkId(), loginUser.getEnterpriseId(), loginUser.getWxEnterpriseId());
        return RestResponse.successResult(serviceResponse.getResult());
    }


    @RequestMapping(path = "/check/name")
    public RestResponse<?> checkGroupName(HmGroupSettingVO groupSettingVO){
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        if (StringUtils.isBlank(groupSettingVO.getGroupName())){
            return RestResponse.successResult(Collections.emptyList());
        }
        ServiceResponse<List<HmGroupDTO>> serviceResponse = groupApiService.queryGroupListByName(loginUser.getWxEnterpriseId(), loginUser.getEnterpriseId(), groupSettingVO.getGroupName());
        if (serviceResponse.isSuccess()){
            return RestResponse.successResult(serviceResponse.getResult());
        }
        return RestResponse.failure(serviceResponse.getCode(), serviceResponse.getMessage());
    }
}
