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

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.yeejoin.amos.boot.biz.common.controller.BaseController;
import com.yeejoin.amos.boot.module.jyjc.biz.service.impl.CommonserviceImpl;
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;

    /**
     * 新增
     *
     * @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 = "/getUserPhonesByPersonCode")
    @ApiOperation(httpMethod = "GET", value = "根据公司的unitCode获取检测检验人员联系电话", notes = "根据公司的unitCode获取检测检验人员联系电话")
    public ResponseModel<Map<String,Object>> getUserPhonesByPersonCode(@RequestParam String unitCode) {
        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) {
        List<TzsUserInfo> userPhones = commonserviceImpl.getUserPhonesByPersonCode(unitCode);
        if (CollectionUtils.isEmpty(userPhones)){
            return null;
        }
        List<String> phones = userPhones.stream().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));
    }

}
