package com.yeejoin.amos.boot.module.jyjc.biz.controller;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.yeejoin.amos.boot.biz.common.controller.BaseController;
import com.yeejoin.amos.boot.module.common.biz.utils.CommonResponseUtil;
import com.yeejoin.amos.boot.module.jyjc.biz.service.TaskModelServiceImpl;
import com.yeejoin.amos.boot.module.jyjc.biz.service.impl.CommonserviceImpl;
import com.yeejoin.amos.boot.module.jyjc.biz.service.impl.EquipRegularlyRemindServicelmpl;
import com.yeejoin.amos.boot.module.ymt.api.entity.TzBaseEnterpriseInfo;
import com.yeejoin.amos.boot.module.ymt.api.entity.TzsUserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.typroject.tyboot.core.foundation.enumeration.UserType;
import org.typroject.tyboot.core.restful.doc.TycloudOperation;
import org.typroject.tyboot.core.restful.utils.ResponseHelper;
import org.typroject.tyboot.core.restful.utils.ResponseModel;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author system_generator
 * @date 2023-12-13
 */
@RestController
@Api(tags = "公共接口controller")
@RequestMapping(value = "/common")
public class CommonController extends BaseController {

    @Autowired
    CommonserviceImpl commonserviceImpl;
    @Autowired
    TaskModelServiceImpl taskModelService;
    @Autowired
    EquipRegularlyRemindServicelmpl equipRegularlyRemindServicelmpl;

    /**
     * 新增
     *
     * @return
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @PostMapping(value = "/getJyjcPersonByUnitCode")
    @ApiOperation(httpMethod = "GET", value = "根据公司的unitCode获取检测检验人员列表", notes = "根据公司的unitCode获取检测检验人员列表")
    public ResponseModel<List<TzsUserInfo>> getUserInfosByUnitCode(@RequestParam String unitCode) {
        return ResponseHelper.buildResponse(commonserviceImpl.getUserInfosByUnitCode(unitCode));
    }

    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @PostMapping(value = "/getBizUserInfosByUnitCode")
    @ApiOperation(httpMethod = "GET", value = "根据公司的unitCode获取检测检验业务办理人员列表", notes = "根据公司的unitCode获取检测检验人员列表")
    public ResponseModel<List<TzsUserInfo>> getBizUserInfosByUnitCode(@RequestParam String unitCode) {
        return ResponseHelper.buildResponse(commonserviceImpl.getBizUserInfosByUnitCode(unitCode));
    }


    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @PostMapping(value = "/getUserPhonesByPersonCode")
    @ApiOperation(httpMethod = "GET", value = "根据公司的unitCode获取检测检验人员联系电话", notes = "根据公司的unitCode获取检测检验人员联系电话")
    public ResponseModel<Map<String,Object>> getUserPhonesByPersonCode(@RequestParam String unitCode) {
        if (unitCode.contains("_")) {
            unitCode = unitCode.split("_")[0];
        }
        List<TzsUserInfo> userPhones = commonserviceImpl.getUserPhonesByPersonCode(unitCode);
        if (CollectionUtils.isEmpty(userPhones)){
            return null;
        }
        List<String> phones = userPhones.stream().map(TzsUserInfo::getPhone).collect(Collectors.toList());
        String phone = String.join(",", phones);
        Map<String,Object> map = new HashMap<>();
        map.put("phone",phone);
        return ResponseHelper.buildResponse(map);
    }


    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @PostMapping(value = "/getInnerPersonCodeByPersonCode")
    @ApiOperation(httpMethod = "POST", value = "根据公司的unitCode获取检测检验人员内部编号", notes = "根据公司的unitCode获取检测检验人员内部编号")
    public ResponseModel<Map<String,Object>> getInnerPersonCodeByPersonCode(@RequestParam String unitCode) {
        if (unitCode.contains("_")) {
            unitCode = unitCode.split("_")[0];
        }
        List<TzsUserInfo> userPhones = commonserviceImpl.getUserPhonesByPersonCode(unitCode);
        if (CollectionUtils.isEmpty(userPhones)){
            return null;
        }
        List<String> phones = userPhones.stream().filter(e-> StringUtils.isNotEmpty(e.getInnerPersonCode())).map(TzsUserInfo::getInnerPersonCode).collect(Collectors.toList());
        Map<String,Object> map = new HashMap<>();
        if (CollectionUtils.isEmpty(phones)){
            map.put("innerPersonCode"," ");
        }else { String innerPersonCode = String.join(",", phones);
            map.put("innerPersonCode",innerPersonCode);
        }

        return ResponseHelper.buildResponse(map);
    }

    /**
     * 查询检验检测机构列表（基本信息及联系人）
     *
     * @return
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @GetMapping(value = "/getInspectionUnitList")
    @ApiOperation(httpMethod = "GET", value = "查询检验检测机构列表（基本信息及联系人）", notes = "查询检验检测机构列表（基本信息及联系人）")
    public ResponseModel<List<TzBaseEnterpriseInfo>> getInspectionUnitList() {
        return ResponseHelper.buildResponse(commonserviceImpl.getInspectionUnitList());
    }

    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @GetMapping(value = "/getInspectionUnit/{sequenceNbr}")
    @ApiOperation(httpMethod = "GET", value = "查询检验检测机构基本信息", notes = "查询检验检测机构基本信息")
    public ResponseModel<TzBaseEnterpriseInfo> getInspectionUnitBySequenceNbr(@PathVariable(value = "sequenceNbr") Long sequenceNbr) {
        return ResponseHelper.buildResponse(commonserviceImpl.getInspectionUnitBySequenceNbr(sequenceNbr));
    }


    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @GetMapping(value = "/test1")
    @ApiOperation(httpMethod = "GET", value = "查询检验检测机构基本信息", notes = "查询检验检测机构基本信息")
    public ResponseModel sendReminderMessage() {
        equipRegularlyRemindServicelmpl.sendReminderMessage();
        return CommonResponseUtil.success();
    }


    /**
     * 公共转办接口
     * /common/transfer
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @PostMapping(value = "/bj/transfer")
    @ApiOperation(httpMethod = "POST", value = "公共转办接口", notes = "公共转办接口")
    public ResponseModel<Object> transferBJ(@RequestBody Map<String, Object> map) {
        map.put("key","instance_id");
        Object result = taskModelService.transfer(map);
        return ResponseHelper.buildResponse(result);
    }


    /**
     * 公共转办接口
     * /common/transfer
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @PostMapping(value = "/kt/transfer")
    @ApiOperation(httpMethod = "POST", value = "公共转办接口", notes = "公共转办接口")
    public ResponseModel<Object> transferKT(@RequestBody Map<String, Object> map) {
        map.put("key","workflow_prostance_id");
        Object result = taskModelService.transfer(map);
        return ResponseHelper.buildResponse(result);
    }
}
