package com.yifu.cloud.plus.v1.yifu.archives.controller;

import com.yifu.cloud.plus.v1.yifu.archives.entity.TBusinessEmployeeExtendInfo;
import com.yifu.cloud.plus.v1.yifu.archives.service.TBusinessEmployeeExtendInfoService;
import com.yifu.cloud.plus.v1.yifu.archives.service.TEmployeeContractInfoService;
import com.yifu.cloud.plus.v1.yifu.archives.service.TEmployeeInfoService;
import com.yifu.cloud.plus.v1.yifu.archives.vo.TSettleDomainListVo;
import com.yifu.cloud.plus.v1.yifu.archives.vo.UpdateEmployeeExtendRedundancyDTO;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import com.yifu.cloud.plus.v1.yifu.common.security.annotation.Inner;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * B端员工档案相关接口
 * @Author fxj
 * @Date 2020-08-25
 * @return
 **/
@RestController
@AllArgsConstructor
@RequestMapping(value = "/customerBusiness/temployeeBusiness")
@Tag(name = "员工档案B端接口")
@Slf4j
public class EmployeeBusinessController {

    private final TEmployeeInfoService tEmployeeInfoService;

    private final TEmployeeContractInfoService tEmployeeContractInfoService;

    private final TBusinessEmployeeExtendInfoService businessEmployeeExtendInfoService;

//    @ApiOperation(value = "B端花名册简单分页查询")
//    @GetMapping("/page")
//    public R<IPage<BusinessEmployeeExtendListVO>> getTEmployeeBusinessPage(Page page, THroBusinessUserQuery query) {
//        ExtendUser user = SecurityUtils.getUser();
//        if (user == null) {
//            return R.fail("请登录!");
//        }
//        if (!R.isSuccess(businessPowerCheck(query))) {
//            return R.fail(businessPowerCheck(query).getMsg());
//        }
//        return new R<>(tEmployeeInfoService.getTEmployeeBusinessPage(page, query));
//    }
//
//    /**
//     * 通过id查询单条记录
//     *
//     * @param id
//     * @return R
//     */
//    @ApiOperation(value = "B端获取员工的相关信息")
//    @GetMapping("/getEmployeeBusinessInfoById")
//    public R<EmployeeBusinessDetailVo> getEmployeeBusinessInfoById(@RequestParam(value = "id",required = true) String id, @RequestParam("settleDomain") String settleDomain) {
//        if (!Common.isNotKong(id)){
//            return R.fail("员工ID不可为空！");
//        }
//        return new R<>(tEmployeeInfoService.getEmployeeBusinessInfoById(id, settleDomain));
//    }
//
//    /**
//     * 通过id查询单条记录
//     *
//     * @param id
//     * @return R
//     */
//    @ApiOperation(value = "B端获取员工及B端附属信息")
//    @GetMapping("/getEmployeeBusinessVoById")
//    public R<EmployeeBusinessVo> getEmployeeBusinessVoById(@RequestParam(value = "id",required = true) String id) {
//        if (!Common.isNotKong(id)){
//            return R.fail("员工ID不可为空！");
//        }
//        return tEmployeeInfoService.getEmployeeBusinessVoById(id);
//    }
//
//    /**
//     * b端人员信息编辑拓展信息
//     * @description: b端人员信息编辑拓展信息
//     * @param dto
//     * @return: com.yifu.cloud.v1.common.core.util.R<java.lang.Boolean>
//     * @author: wangweiguo
//     * @date: 2021/8/10
//     */
//    @ApiOperation(value = "编辑：b端人员信息编辑拓展信息")
//    @PutMapping("/updateEmployInfoByExtendId")
//    public R<Boolean> updateEmployInfoByExtendId(@RequestBody @Valid UpdateBusinessEmployeeExtendDTO dto) {
//        TBusinessEmployeeExtendInfo tBusinessEmployeeExtendInfo = this.businessEmployeeExtendInfoService.getDataById(dto.getExtendId());
//        if (null == tBusinessEmployeeExtendInfo) {
//            tBusinessEmployeeExtendInfo = new TBusinessEmployeeExtendInfo();
//        }
//        tBusinessEmployeeExtendInfo.setId(dto.getExtendId());
//        BeanUtil.copyProperties(dto, tBusinessEmployeeExtendInfo, CopyOptions.create().setIgnoreNullValue(true));
//        if (StringUtils.isBlank(tBusinessEmployeeExtendInfo.getEmpName())) {
//            TEmployeeInfo employeeInfo = this.tEmployeeInfoService.lambdaQuery().eq(TEmployeeInfo::getEmpIdcard, dto.getEmpIdcard()).last(CommonConstants.SQL_LIMIT_ONE).one();
//            if (null != employeeInfo) {
//                tBusinessEmployeeExtendInfo.setEmpName(employeeInfo.getEmpName());
//            }
//        }
//        return businessEmployeeExtendInfoService.saveOrUpdateData(tBusinessEmployeeExtendInfo);
//    }
//
//    /**
//     * b端人员信息批量导入更新人员信息拓展表
//     * @description: 批量更新：b端人员信息批量导入更新人员信息拓展表
//     * @param jsonStr
//     * @return: com.yifu.cloud.v1.common.core.util.R<java.lang.Boolean>
//     * @author: wangweiguo
//     * @date: 2021/8/10
//     */
//    @ApiOperation(value = "批量更新：b端人员信息批量导入更新人员信息拓展表")
//    @PutMapping("/batchUpdateEmployeeExtendByJsonStr")
//    public R<Boolean> batchUpdateEmployeeExtendByJsonStr(@RequestBody String jsonStr) {
//        ExtendUser user = SecurityUtils.getUser();
//        if (user == null) {
//            return R.fail(CommonConstants.USER_FAIL);
//        }
//        if (CollUtil.isEmpty(user.getSettleIdList())) {
//            return R.fail(BasicCommonErrorMsg.NO_SETTLE_DOMAIN_AUTH);
//        }
//        List<TEmployeeInfo> employeeInfoList = tEmployeeInfoService.lambdaQuery().in(TEmployeeInfo::getSettleDomain, user.getSettleIdList()).list();
//        if (CollUtil.isEmpty(employeeInfoList)) {
//            return R.fail(HRO_EMPLOYEE_NULL);
//        }
//
//        HashMap<String, TEmployeeInfo> empInfoMap = Common.listToHashMapByKey(employeeInfoList, "empIdcard");
//        return businessEmployeeExtendInfoService.batchUpdateEmployeeExtendByJsonStr(jsonStr, empInfoMap);
//    }
//
//    /**
//     * b端人员信息导出
//     * @description: 导出：b端人员信息导出
//     * @param query
//     * @return: com.yifu.cloud.v1.common.core.util.R<java.lang.Boolean>
//     * @author: wangweiguo
//     * @date: 2021/8/10
//     */
//    @ApiOperation(value = "导出：b端人员信息导出")
//    @GetMapping("/exportByParams")
//    public R<List<BusinessEmployeeExtentExcelVO>> exportByParams(THroBusinessUserQuery query) {
//        if (!R.isSuccess(businessPowerCheck(query))) {
//            return R.fail(businessPowerCheck(query).getMsg());
//        }
//        return this.tEmployeeInfoService.exportByParams(query);
//    }

    /**
     * 更新人员信息冗余字段
     * @description: 更新人员信息冗余字段
     * @param dto
     * @return: com.yifu.cloud.v1.common.core.util.R<java.lang.List<ErrorMessage>>
     * @author: wangweiguo
     * @date: 2021/8/12
     */
    @Inner
    @PostMapping("/inner/updateEmployeeExtendRedundancy")
    public Boolean updateEmployeeExtendRedundancy(@RequestBody UpdateEmployeeExtendRedundancyDTO dto) {
        return this.businessEmployeeExtendInfoService.updateEmployeeExtendRedundancy(dto);
    }

//    /**
//     * 按身份证单个更新证件冗余信息
//     * @author: fxj
//     * @date 2021-08-23
//     * @param dto
//     * @return
//     */
//    @Inner
//    @PostMapping("/inner/updateEmpExtendCertInfo")
//    public R<String> updateEmpExtendCertInfo(@RequestBody UpdateEmployeeExtendRedundancyDTO dto) {
//        return this.businessEmployeeExtendInfoService.updateEmpExtendCertInfo(dto);
//    }
//
//    /**
//     * 根据身份证号获取拓展信息列表
//     * @description: 根据身份证号获取拓展信息列表
//     * @param idcardList
//     * @return: com.yifu.cloud.v1.common.core.util.R<List<TBusinessEmployeeExtendInfo>>
//     * @author: wangweiguo
//     * @date: 2021/8/16
//     */
//    @Inner
//    @PostMapping("/inner/getListByIdcartList")
//    public R<List<TBusinessEmployeeExtendInfo>> getListByIdcartList(@RequestBody List<String> idcardList) {
//        if (CollUtil.isEmpty(idcardList)) {
//            return R.fail("身份证号列表为空");
//        }
//        return this.businessEmployeeExtendInfoService.getListByIdcartList(idcardList);
//    }
//
//    /**
//     * 查询-列表：内部服务调用，获取所有非离职员工拓展表信息
//     * @description: 查询-列表：内部服务调用，获取所有非离职员工拓展表信息
//     * @return: com.yifu.cloud.v1.common.core.util.R<java.util.List<com.yifu.cloud.v1.hrms.api.vo.basic.BusinessEmployeeExtendVO>>
//     * @author: wangweiguo
//     * @date: 2021/8/17
//     */
//    @Inner
//    @GetMapping("/inner/getNotLeaveExtendList")
//    public R<List<BusinessEmployeeExtendVO>> getNotLeaveExtendList() {
//        return this.businessEmployeeExtendInfoService.getNotLeaveExtendList();
//    }
//
    /**
     * 查询-列表：内部服务调用，获取所有员工拓展表信息
     * @description: 查询-列表：内部服务调用，获取所有非离职员工拓展表信息
     * @return: R<List<TBusinessEmployeeExtendInfo>>
     * @author: wangweiguo
     * @date: 2021/8/25
     */
    @Inner
    @PostMapping("/inner/getAllExtendList")
    public TSettleDomainListVo getAllExtendList() {
		TSettleDomainListVo vo = new TSettleDomainListVo();
		List<TBusinessEmployeeExtendInfo> list = this.businessEmployeeExtendInfoService.list();
		vo.setExtendInfo(list);
        return vo;
    }

    /**
     * 查询-列表：内部服务调用，根据电工编号查询扩展表信息
     * @return: R<TBusinessEmployeeExtendInfo>
     * @author: wangweiguo
     * @date: 2021/8/31
     */
	@Inner
    @PostMapping("/inner/getExtendByBusinessTelecomNumber")
	R<TBusinessEmployeeExtendInfo> getExtendByBusinessTelecomNumber(@RequestParam("businessTelecomNumber") String businessTelecomNumber) {
        TBusinessEmployeeExtendInfo businessEmployeeExtendInfo = this.businessEmployeeExtendInfoService.lambdaQuery()
                .eq(TBusinessEmployeeExtendInfo::getBusinessTelecomNumber, businessTelecomNumber)
                .last(CommonConstants.LAST_ONE_SQL)
                .one();
        return R.ok(businessEmployeeExtendInfo,CommonConstants.SEARCH_SUCCESS);
    }

//    /**
//     * @param
//     * @Description: 获取所有附属信息
//     * @Author: hgw
//     * @Date: 2021/8/18 16:46
//     * @return: com.yifu.cloud.v1.common.core.util.R<java.util.Map < java.lang.String, com.yifu.cloud.v1.hrms.api.vo.basic.BusinessEmployeeExtendByLeave>>
//     **/
//    @Inner
//    @GetMapping("/inner/getInfoByLeaveMap")
//    public R<Map<String, BusinessEmployeeExtendByLeave>> getInfoByLeaveMap() {
//        return this.businessEmployeeExtendInfoService.getInfoByLeaveMap();
//    }
//
//
//    /**
//     * @description: 校验数据权限
//     * @param query
//     * @return: com.yifu.cloud.v1.common.core.util.R
//     * @author: wangweiguo
//     * @date: 2021/8/11
//     */
//    private R businessPowerCheck(THroBusinessUserQuery query) {
//        ExtendUser user = SecurityUtils.getUser();
//        if (null == query){
//            query = new THroBusinessUserQuery();
//        }
//        // 如果有姓名 按姓名查询  清空结算主体（产品要求结算主体和姓名查询互斥）
//        // hrob2.0 去掉该判断，因为能查到不是b端的人员信息 --wwg 2021-08-12
////        if (Common.isNotKong(query.getEmpName())){
////            query.setSettleDomainId(null);
////            return R.success("success");
////        }
//        List<String> settleDomainIds = new ArrayList<>();
//        boolean flag =  !SecurityUtils.isHaveAllOrg(ServiceNameConstants.CLIENT_ID_HR_B, user);
//        if (flag){
//            settleDomainIds = user.getSettleIdList();
//            if (Common.isEmpty(settleDomainIds)) {
//                return R.fail(EmpBusinessConstants.noSettleDomainAuth);
//            }
//            query.setSettleDomainIds(settleDomainIds);
//            if (Common.isNotKong(query.getSettleDomainId())){
//                // 无权限提示  有权限 按结算主体/项目ID 查询
//                if (ServiceUtil.checkUserAuth(query.getSettleDomainId(),settleDomainIds)){
//                    return R.fail(EmpBusinessConstants.noSettleDomainAuth);
//                }
//            }
//            // 管理员权限特殊处理
//        }else {
//            if (Common.isEmpty(query.getSettleDomainId()) && Common.isEmpty(query.getDepartId())){
//                return R.fail("项目和部门不可同时为空");
//            }
//        }
//        if (Common.isNotKong(settleDomainIds)){
//            query.setSettleDomainIds(settleDomainIds);
//        }
//        return R.success("success");
//    }
//
//    /**
//     * B端合同列表查询接口
//     * @Author fxj
//     * @Date 2020-08-25
//     * @param page
//     * @param contractBusinessPageVo
//     * @param flag  0 按3个月内到期提醒  1 不安提醒查询
//     * @return
//     * @see com.yifu.cloud.v1.hr.basicArchives.controller
//     **/
//    @ApiOperation(value = "B端合同列表查询接口（flag： 0-按3个月内到期提醒 1-不安提醒查询）")
//    @GetMapping("/getContractBusinessPage")
//    public R<IPage<EmpContractBusinessPageVo>> getContractBusinessPage(Page<EmpContractBusinessPageVo> page, EmpContractBusinessPageVo contractBusinessPageVo, Integer flag) {
//        if (null == flag){
//            flag = 1;
//        }
//        ExtendUser user = SecurityUtils.getUser();
//        if (user == null) {
//            return R.fail("请登录!");
//        }
//        if (null == contractBusinessPageVo){
//            contractBusinessPageVo =  new EmpContractBusinessPageVo();
//        }
//        contractBusinessPageVo.setIsObsolete(CommonConstants.ZERO);
//        // 如果有姓名 按姓名查询  清空结算主体（产品要求结算主体和姓名查询互斥）
//        if (Common.isNotKong(contractBusinessPageVo.getEmpName())){
//            contractBusinessPageVo.setSettleDomain(null);
//        }
//        List<String> settleDomainIds = new ArrayList<>();
//        if (!SecurityUtils.isHaveAllOrg(ServiceNameConstants.CLIENT_ID_HR_B, user)){
//            settleDomainIds = user.getSettleIdList();
//            if (Common.isEmpty(settleDomainIds)) {
//                return R.fail(EmpBusinessConstants.noSettleDomainAuth);
//            }
//            if (Common.isNotKong(contractBusinessPageVo.getSettleDomain())){
//                // 无权限提示  有权限 按结算主体/项目ID 查询
//                if (ServiceUtil.checkUserAuth(contractBusinessPageVo.getSettleDomain(),settleDomainIds)){
//                    return R.fail(EmpBusinessConstants.noSettleDomainAuth);
//                }
//                settleDomainIds = null;
//            }
//            // 管理员权限特殊处理
//        }else {
//            if (Common.isEmpty(contractBusinessPageVo.getSettleDomain())){
//                return new R<>(null);
//            }
//        }
//        return new R<>(tEmployeeContractInfoService.getContractBusinessPage(page, contractBusinessPageVo, settleDomainIds, flag));
//    }
//
//    /**
//     * B端查询合同到期提醒数（查询3个月内到期的合同）
//     * @Author fxj
//     * @Date 2020-08-25
//     * @param
//     * @return
//     * @see com.yifu.cloud.v1.hr.basicArchives.controller
//     **/
//    @ApiOperation(value = "B端查询合同到期提醒数（查询3个月内到期的合同）")
//    @GetMapping("/getContractBusinessAlertCount")
//    public R<Integer> getContractBusinessAlertCount() {
//        ExtendUser user = SecurityUtils.getUser();
//        if (user == null) {
//            return R.fail("请登录!");
//        }
//        List<String> settleDomainIds = new ArrayList<>();
//        if (!SecurityUtils.isHaveAllOrg(ServiceNameConstants.CLIENT_ID_HR_B, user)){
//            settleDomainIds = user.getSettleIdList();
//            if (settleDomainIds == null || settleDomainIds.isEmpty()) {
//                return R.fail(EmpBusinessConstants.noSettleDomainAuth);
//            }
//        }
//        return new R<>(tEmployeeContractInfoService.getContractBusinessAlertCount(settleDomainIds));
//    }
//
//    /**
//     * B端查询对应员工指定合同ID之外的其他合同信息
//     * @Author fxj
//     * @Date 2020-08-25
//     * @param empId
//     * @param contractId
//     * @return
//     * @see com.yifu.cloud.v1.hr.basicArchives.controller
//     **/
//    @ApiOperation(value = "B端查询对应员工指定合同ID之外的其他合同信息")
//    @GetMapping("/getOtherContractBusinessInfo")
//    public R<List<EmpContractBusinessPageVo>> getOtherContractBusinessInfo(@RequestParam(value = "empId",required = true)String empId,@RequestParam(value = "contractId", required = true)String contractId) {
//        return new R<>(tEmployeeContractInfoService.getOtherContractBusinessInfo(empId,contractId));
//    }
//
//    /**
//     * @param businessTelecomNumber
//     * @Description: 根据电信编号，获取附属信息
//     * @Author: hgw
//     * @Date: 2021/8/18 17:06
//     * @return: com.yifu.cloud.v1.common.core.util.R<com.yifu.cloud.v1.hrms.api.entity.basic.TBusinessEmployeeExtendInfo>
//     **/
//    @ApiOperation(value = "根据电信编号，获取附属信息")
//    @GetMapping("/getByBusinessTelecomNumber")
//    public R<TBusinessEmployeeExtendInfo> getByBusinessTelecomNumber(@RequestParam String businessTelecomNumber) {
//        return new R<>(businessEmployeeExtendInfoService.getByBusinessTelecomNumber(businessTelecomNumber));
//    }

}
