package com.diagbot.facade;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.diagbot.dto.*;
import com.diagbot.entity.MedicalRecord;
import com.diagbot.entity.MedicalRecordContent;
import com.diagbot.enums.IsDeleteEnum;
import com.diagbot.exception.CommonErrorCode;
import com.diagbot.exception.CommonException;
import com.diagbot.facade.data.AMedicalRecordContentFacade;
import com.diagbot.service.impl.MedicalRecordServiceImpl;
import com.diagbot.util.BeanUtil;
import com.diagbot.util.EncrypDES;
import com.diagbot.util.StringUtil;
import com.diagbot.util.SysUserUtils;
import com.diagbot.vo.MedRecordContentOtherVO;
import com.diagbot.vo.MedRecordOtherVO;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @Description:
 * @author: Wsy
 * @time: 2022/3/9 11:39
 */
@Component
public class MedicalRecordOtherFacade extends MedicalRecordServiceImpl {

    @Autowired
    private MedicalRecordFacade medicalRecordFacade;

    @Autowired
    private AMedicalRecordContentFacade AMedicalRecordContentFacade;

    @Value("${encrypt.enable}")
    Boolean encryptFlag;

    /**
     * @param medRecordOtherVO
     * @return
     */
    public IPage<MedicalRecord> pageFac(MedRecordOtherVO medRecordOtherVO) {
        Boolean flag = false;
        QueryWrapper<MedicalRecord> medicalRecordQueryWrapper = new QueryWrapper<>();
        medicalRecordQueryWrapper.eq("is_deleted", IsDeleteEnum.N.getKey());
        medicalRecordQueryWrapper.eq("hospital_id", Long.valueOf(SysUserUtils.getCurrentHospitalID()));
        medicalRecordQueryWrapper.eq("mode_id", 0);
        if (medRecordOtherVO != null) {
            medicalRecordQueryWrapper.eq("behospital_code", medRecordOtherVO.getBehospitalCode());
        }

        if (medRecordOtherVO.getRecTitle() != null && medRecordOtherVO != null) {
            medicalRecordQueryWrapper.like("rec_title", medRecordOtherVO.getRecTitle());
        }
        if (medRecordOtherVO.getRecDateStart() != null && medRecordOtherVO != null) {
            medicalRecordQueryWrapper.ge("rec_date", medRecordOtherVO.getRecDateStart());
        }
        if (medRecordOtherVO.getRecDateEnd() != null && medRecordOtherVO != null) {
            medicalRecordQueryWrapper.le("rec_date", medRecordOtherVO.getRecDateEnd());
        }
        for (Object order : medRecordOtherVO.getOrders()) {
            ObjectMapper objectMapper = new ObjectMapper();
            OrderItem orderName = objectMapper.convertValue(order, OrderItem.class);
            String column = orderName.getColumn();
            if (StringUtil.isNotEmpty(column) && orderName.isAsc()) {
                flag = true;
            }
        }
        if (flag) {
            medicalRecordQueryWrapper.orderByAsc("rec_date");
        } else {
            medicalRecordQueryWrapper.orderByDesc("rec_date");
        }

        Page<MedicalRecord> pageCase = new Page<MedicalRecord>(medRecordOtherVO.getCurrent(), medRecordOtherVO.getSize());
        IPage<MedicalRecord> medicalRecordIPage = medicalRecordFacade.getBaseMapper().selectPage(pageCase, medicalRecordQueryWrapper);

        return medicalRecordIPage;
    }

    /**
     * 文书详情
     */
    public MedRecordContentOtherDTO getRecordContentOther(MedRecordContentOtherVO medRecordContentOtherVO) {
        MedicalRecordContent medicalRecordContent = AMedicalRecordContentFacade.getOne(new QueryWrapper<MedicalRecordContent>()
                .eq("hospital_id", Long.valueOf(SysUserUtils.getCurrentHospitalID()))
                .eq("rec_id", medRecordContentOtherVO.getRecId())
                .eq("is_deleted", IsDeleteEnum.N.getKey())
        );
        if (medicalRecordContent != null) {
            if (medicalRecordContent.getHospitalId() == 35) {
                medicalRecordContent.setHtmlText(medicalRecordContent.getXmlText());
            }
        } else {
            throw new CommonException(CommonErrorCode.PARAM_IS_ERROR, "该文书数据缺失~");
        }
        MedRecordContentOtherDTO medRecordContentOtherDTO = new MedRecordContentOtherDTO();
        BeanUtil.copyProperties(medicalRecordContent, medRecordContentOtherDTO);
        // 解密数据
        if (encryptFlag) {
            try {
                EncrypDES encrypDES = new EncrypDES();
                if (StringUtils.isNotEmpty(medRecordContentOtherDTO.getHtmlText())) {
                    medRecordContentOtherDTO.setHtmlText(encrypDES.decryptor(medRecordContentOtherDTO.getHtmlText()));
                }
            } catch (Exception e) {
                medRecordContentOtherDTO.setHtmlText(null);
                throw new CommonException(CommonErrorCode.SERVER_IS_ERROR,
                        "解密错误!病历ID=【" + medRecordContentOtherDTO.getRecId() + "】");
            }
        }
        return medRecordContentOtherDTO;
    }

}
