package cn.hnthyy.thmz.controller.mz;

import cn.hnthyy.thmz.comment.UserLoginToken;
import cn.hnthyy.thmz.entity.his.mz.Employee;
import cn.hnthyy.thmz.service.his.mz.EmployeeService;
import cn.hnthyy.thmz.service.his.zd.ZdUnitCodeService;
import lombok.extern.slf4j.Slf4j;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@Slf4j
@RestController
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private ZdUnitCodeService zdUnitCodeService;
    /**
     * 根据科室列表查询医生列表
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/listEmployeeByDepts", method = {RequestMethod.GET, RequestMethod.POST})
    public Map<String, Object> listEmployeeByDepts(@RequestParam("depts") String depts) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            List<Employee> employeeList=employeeService.queryEmployeeByDepts(depts);
            employeeList.stream().forEach(e->{
                e.setDeptName(zdUnitCodeService.queryDeptNameByIdInCache(e.getDeptCode()));
            });
            resultMap.put("code", 0);
            resultMap.put("message", "根据科室列表查询医生列表成功");
            resultMap.put("data", employeeList);
            return resultMap;
        }catch (Exception e){
            e.printStackTrace();
            log.error("根据科室列表查询医生列表失败,错误信息{}",e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "根据科室列表查询医生列表失败");
            return resultMap;
        }
    }


    /**
     * 根据科室id查询医生列表
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/listEmployeeByDept", method = {RequestMethod.GET, RequestMethod.POST})
    public Map<String, Object> listEmployeeByDept(@RequestParam("dept") String dept) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            resultMap.put("code", 0);
            resultMap.put("message", "根据科室id查询医生列表成功");
            if(StringUtils.isBlank(dept)){
                dept=null;
            }
            resultMap.put("data", employeeService.queryEmployeeByDept(dept));
            return resultMap;
        }catch (Exception e){
            e.printStackTrace();
            log.error("根据科室id查询医生列表失败,错误信息{}",e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "根据科室id查询医生列表失败");
            return resultMap;
        }
    }
}
