package com.diagbot.facade.data;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diagbot.dto.RespDTO;
import com.diagbot.dto.data.AMedicalRecordTypeDTO;
import com.diagbot.entity.MedRecordType;
import com.diagbot.enums.IsDeleteEnum;
import com.diagbot.service.impl.MedRecordTypeServiceImpl;
import com.diagbot.util.BeanUtil;
import com.diagbot.util.TZDBConn;
import com.diagbot.vo.data.AMedicalRecordTypeVO;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class AMedRecordTypeFacade extends MedRecordTypeServiceImpl {

    @Autowired
    @Qualifier("medRecordTypeServiceImpl")
    private MedRecordTypeServiceImpl medRecordTypeService;
    @Autowired
    private AMedAbnormalInfoFacade aMedAbnormalInfoFacade;

    private TZDBConn tzDBConn = new TZDBConn();

    /**
     * 终末质控-同步前一天变更的模板类型
     */
    public void executeMRType() {
        //String sql="select * from ar_medirecclass where SFZFPB='1'";
        String sql="select * from ar_medirecclass where SFZFPB='1' and cjcxrq>=(select CONVERT(varchar,GETDATE()-2,23)) and cjcxrq<(select CONVERT(varchar,GETDATE(),23))";
        List<MedRecordType> medRecordTypeList = tzDBConn.getMedicalRecordType(sql);
        execute(medRecordTypeList);
    }
    /**
     * 数据引擎-通过接口更新病人住院记录信息
     * @param list
     * @return
     */
    public RespDTO<List<AMedicalRecordTypeDTO>> executeMRType(List<AMedicalRecordTypeVO> list) {
        if(list!=null && list.size()>0){
            //循环验证数据有效性
            for (AMedicalRecordTypeVO medRecordType:list) {
                if("".equals(medRecordType.getTypeId())) {
                    return RespDTO.onError("请输入文书类型编码!");
                }else if(medRecordType.getHospitalId()==null){
                    return RespDTO.onError("请输入医院编码!");
                }else if("".equals(medRecordType.getTypeName())){
                    return RespDTO.onError("请文书类型名称!");
                }
            }
            List<MedRecordType> medRecordTypeList=Lists.newArrayList();
            List<AMedicalRecordTypeDTO> medRecordTypeDtoList=Lists.newArrayList();
            medRecordTypeList=BeanUtil.listCopyTo(list,MedRecordType.class);
            medRecordTypeDtoList=BeanUtil.listCopyTo(list,AMedicalRecordTypeDTO.class);
            execute(medRecordTypeList);
            return RespDTO.onSuc(medRecordTypeDtoList);
        }else{
            return RespDTO.onError("未接收到数据!");
        }
    }

    public void execute(List<MedRecordType> medRecordTypeList){
        try {
            List<MedRecordType> addE = Lists.newLinkedList();
            List<MedRecordType> updateE = Lists.newLinkedList();
            if (medRecordTypeList != null && medRecordTypeList.size() != 0) {
                medRecordTypeList.stream().forEach(s -> {
                    MedRecordType mrType = this.getOne(new QueryWrapper<MedRecordType>()
                            .eq("type_id", s.getTypeId())
                            .eq("hospital_id", s.getHospitalId())
                            .eq("is_deleted",IsDeleteEnum.N), false);
                    if (mrType != null) {
                        s.setGmtModified(new Date());
                        updateE.add(s);
                    } else {
                        s.setGmtCreate(new Date());
                        addE.add(s);
                    }
                });
            }
            if(addE.size()>0){
                medRecordTypeService.saveBatch(addE);
            }
            if(updateE.size()>0){
                medRecordTypeService.updateBatchByKey(updateE);
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
            aMedAbnormalInfoFacade.saveAbnormalInfo("文书类别","", JSON.toJSONString(medRecordTypeList),"",e.getMessage());
        }

    }
}
