package com.docus.server.collection.controller;

import cn.hutool.core.util.ObjectUtil;
import com.docus.infrastructure.web.api.CommonResult;
import com.docus.server.collection.dto.*;
import com.docus.server.collection.service.IPowerDeptService;
import com.docus.server.collection.service.IPowerUserService;
import com.docus.server.collection.service.ITBasicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("省中医珠海分院相关接口")
@Slf4j
@RestController
@RequestMapping("/szy")
public class SzyZhController {

    @Autowired
    private ITBasicService tBasicService;

    @Autowired
    private IPowerDeptService powerDeptService;

    @Autowired
    private IPowerUserService powerUserService;

    @ApiOperation("基础数据同步")
    @PostMapping("/setRecord")
    public CommonResult setRecord(@RequestBody List<SzyTBasicDto> dtos) {
        if(ObjectUtil.isEmpty(dtos)){
            return CommonResult.failed("数据为空");
        }
        for (SzyTBasicDto dto : dtos) {
            if(ObjectUtil.isEmpty(dto.getInpatientNo())){
                log.error("住院号为空"+dto.toString());
                return CommonResult.failed("住院号为空");
            }
            if(ObjectUtil.isEmpty(dto.getJzh())){
                log.error("住院号为空"+dto.toString());
                return CommonResult.failed("住院流水号为空");
            }
        }
        tBasicService.setSzyTBasic(dtos);
        return CommonResult.success("同步成功");
    }

    @ApiOperation("科室数据同步")
    @PostMapping("/setDept")
    public CommonResult setDept(@RequestBody List<SzyDeptDto> dtos) {
        if(ObjectUtil.isEmpty(dtos)){
            return CommonResult.failed("数据为空");
        }
        for (SzyDeptDto dto : dtos) {
            if(ObjectUtil.isEmpty(dto.getDeptCode())){
                log.error("科室编号为空"+dto.toString());
                return CommonResult.failed("科室编号为空");
            }
            if(ObjectUtil.isEmpty(dto.getDeptName())){
                log.error("科室名为空"+dto.toString());
                return CommonResult.failed("科室名为空");
            }
            powerDeptService.add(dto);
        }
        return CommonResult.success("同步成功");
    }

    @ApiOperation("用户数据同步")
    @PostMapping("/setUser")
    public CommonResult setUser(@RequestBody List<SzyUserDto> dtos) {
        if(ObjectUtil.isEmpty(dtos)){
            return CommonResult.failed("数据为空");
        }
        for (SzyUserDto dto : dtos) {
            if(ObjectUtil.isEmpty(dto.getUserName())){
                log.error("用户工号为空"+dto.toString());
                return CommonResult.failed("用户工号为空");
            }
            powerUserService.add(dto);
        }
        return CommonResult.success("同步成功");
    }
}
