package com.fs.his.service.impl;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.fs.common.service.impl.SmsServiceImpl;
import com.fs.common.utils.DateUtils;
import com.fs.his.domain.*;
import com.fs.his.dto.FsInquiryOrderPatientDTO;
import com.fs.his.mapper.FsDoctorMapper;
import com.fs.his.mapper.FsPrescribeMapper;
import com.fs.his.mapper.FsStoreOrderMapper;
import com.fs.his.param.FsFollowReportParam;
import com.fs.his.param.FsStoreOrderParam;
import com.fs.his.vo.FsFollowReportListVO;
import com.fs.im.dto.MsgCustomDTO;
import com.fs.im.dto.MsgDTO;
import com.fs.im.dto.MsgDataDTO;
import com.fs.im.dto.MsgDataFormatDTO;
import com.fs.im.service.IImService;
import io.swagger.models.auth.In;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.his.mapper.FsFollowReportMapper;
import com.fs.his.service.IFsFollowReportService;

/**
 * 咨询报告Service业务层处理
 *
 * @author fs
 * @date 2024-01-29
 */
@Service
public class FsFollowReportServiceImpl implements IFsFollowReportService
{
    @Autowired
    private FsFollowReportMapper fsFollowReportMapper;
    @Autowired
    private FsDoctorMapper  doctorMapper;
    @Autowired
    private FsStoreOrderMapper fsStoreOrderMapper;
    @Autowired
    private FsPrescribeMapper fsPrescribeMapper;
    @Autowired
    private IImService imService;
    @Autowired
    SmsServiceImpl smsService;
    Logger logger= LoggerFactory.getLogger(getClass());
    /**
     * 查询咨询报告
     *
     * @param id 咨询报告主键
     * @return 咨询报告
     */
    @Override
    public FsFollowReport selectFsFollowReportById(Long id)
    {
        return fsFollowReportMapper.selectFsFollowReportById(id);
    }

    /**
     * 查询咨询报告列表
     *
     * @param fsFollowReport 咨询报告
     * @return 咨询报告
     */
    @Override
    public List<FsFollowReport> selectFsFollowReportList(FsFollowReport fsFollowReport)
    {
        return fsFollowReportMapper.selectFsFollowReportList(fsFollowReport);
    }

    /**
     * 新增咨询报告
     *
     * @param fsFollowReport 咨询报告
     * @return 结果
     */
    @Override
    public int insertFsFollowReport(FsFollowReport fsFollowReport)
    {
        return fsFollowReportMapper.insertFsFollowReport(fsFollowReport);
    }

    /**
     * 修改咨询报告
     *
     * @param fsFollowReport 咨询报告
     * @return 结果
     */
    @Override
    public int updateFsFollowReport(FsFollowReport fsFollowReport)
    {
        return fsFollowReportMapper.updateFsFollowReport(fsFollowReport);
    }

    /**
     * 批量删除咨询报告
     *
     * @param ids 需要删除的咨询报告主键
     * @return 结果
     */
    @Override
    public int deleteFsFollowReportByIds(Long[] ids)
    {
        return fsFollowReportMapper.deleteFsFollowReportByIds(ids);
    }

    /**
     * 删除咨询报告信息
     *
     * @param id 咨询报告主键
     * @return 结果
     */
    @Override
    public int deleteFsFollowReportById(Long id)
    {
        return fsFollowReportMapper.deleteFsFollowReportById(id);
    }

    @Override
    public List<FsFollowReportListVO> selectFsFollowReportListVO(FsFollowReportParam fsFollowReport) {
        return fsFollowReportMapper.selectFsFollowReportListVO(fsFollowReport);
    }

    @Override
    public void addReport(String userId, String doctorId) {
        List<Long> longs = doctorMapper.selectFollowDoctorListByPackage();
        long doctorIdL = Long.parseLong(doctorId);
        long userIdL = Long.parseLong(userId);
        for (Long aLong : longs) {
            if (aLong==doctorIdL){
                MsgDTO msgDTO1=new MsgDTO();
                MsgCustomDTO customDTO=new MsgCustomDTO();
                //int i = fsFollowReportMapper.selectFollowByUserIdAndDoctorId(userIdL, doctorIdL);
//                if (i==0){
//                    FsInquiryOrder id = fsFollowReportMapper.selectFsInquiryOrderById(userIdL, doctorIdL);
//                    if (id==null){
//                        FsFollow fsFollow = fsFollowReportMapper.selectFsFollowOrderById(userIdL, doctorIdL);
//                        if (fsFollow==null){
//                            return;
//                        }
//                        customDTO.setType("follow");
//                        customDTO.setOrderType(1);
//                        customDTO.setImType(2);
//                        customDTO.setOrderId(fsFollow.getStoreOrderId()+"");
//                        customDTO.setFollowId(fsFollow.getFollowId()+"");
//                        msgDTO1.setCloudCustomData(JSONUtil.toJsonStr(customDTO));
//                        msgDTO1.setFrom_Account("D-"+doctorId);
//                        msgDTO1.setTo_Account("U-"+userId);
//                        List<MsgDataDTO> msgs1=new ArrayList<>();
//                        MsgDataDTO msg1=new MsgDataDTO();
//                        msg1.setMsgType("TIMTextElem");
//                        MsgDataFormatDTO dto1=new MsgDataFormatDTO("请您详细描述您要咨询的问题和目前用药情况；如有近期检查报告，上传近期检查报告和清晰舌苔照片。");
//                        msg1.setMsgContent(dto1);
//                        msgs1.add(msg1);
//                        msgDTO1.setMsgBody(msgs1);
//                        imService.sendMsg(msgDTO1);
//                    }else {
//                        customDTO.setType("startInquiry");
//                        customDTO.setImType(1);
//                        customDTO.setOrderType(id.getOrderType());
//                        customDTO.setOrderId(id.getOrderId()+"");
//                        msgDTO1.setCloudCustomData(JSONUtil.toJsonStr(customDTO));
//                        msgDTO1.setFrom_Account("D-"+doctorId);
//                        msgDTO1.setTo_Account("U-"+userId);
//                        List<MsgDataDTO> msgs1=new ArrayList<>();
//                        MsgDataDTO msg1=new MsgDataDTO();
//                        msg1.setMsgType("TIMTextElem");
//                        MsgDataFormatDTO dto1=new MsgDataFormatDTO("请您详细描述您要咨询的问题和目前用药情况；如有近期检查报告，上传近期检查报告和清晰舌苔照片。");
//                        msg1.setMsgContent(dto1);
//                        msgs1.add(msg1);
//                        msgDTO1.setMsgBody(msgs1);
//                        imService.sendMsg(msgDTO1);
//                    }
//
//
//                }
                List<FsStoreOrder> fsStoreOrders = fsStoreOrderMapper.selectFsStoreOrderByUserIdAndDoctorId(userIdL, doctorIdL);
                for (FsStoreOrder fsStoreOrder : fsStoreOrders) {
                    Long companyId = fsStoreOrder.getCompanyId();
                    Long storeId = fsStoreOrder.getStoreId();
                    String moth = new SimpleDateFormat("yyyyMM").format(new Date());
                    FsFollowReport fsFollowReport = fsFollowReportMapper.selectFsFollowReportByCompanyIdAndUseridAndDoctorId(userIdL, doctorIdL, companyId, moth,storeId);
                    if (fsFollowReport==null){
                        FsFollowReport r = new FsFollowReport();
                        r.setDoctorId(doctorIdL);
                        r.setUserId(userIdL);
                        r.setCompanyId(companyId);
                        r.setCountTime(new Date());
                        r.setMonth(moth);
                        r.setFollowCount(1);

                        if (fsStoreOrder.getPrescribeId()!=null){
                            FsPrescribe fsPrescribe = fsPrescribeMapper.selectFsPrescribeByPrescribeId(fsStoreOrder.getPrescribeId());
                            if (fsPrescribe!=null){
                                r.setPatientName(fsPrescribe.getPatientName());
                            }
                        }

                        r.setStoreId(fsStoreOrder.getStoreId());
                        FsDoctor fsDoctor = doctorMapper.selectFsDoctorByDoctorId(doctorIdL);
                        r.setDoctorName(fsDoctor.getDoctorName());
                        r.setDoctorAccount(fsDoctor.getAccount());
                        fsFollowReportMapper.insertFsFollowReport(r);
                    }else {

                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        String countTime = sdf.format(fsFollowReport.getCountTime());
                        String today = sdf.format(new Date());
                        if (!countTime.equals(today)){
                            FsFollowReport r = new FsFollowReport();
                            if (fsStoreOrder.getPrescribeId()!=null){
                                FsPrescribe fsPrescribe = fsPrescribeMapper.selectFsPrescribeByPrescribeId(fsStoreOrder.getPrescribeId());
                                if (fsPrescribe!=null){
                                    if (fsFollowReport.getPatientName()!=null){
                                        if (!fsFollowReport.getPatientName().contains(fsPrescribe.getPatientName())){
                                            r.setPatientName(fsFollowReport.getPatientName()+","+fsPrescribe.getPatientName());
                                        }
                                    }else {
                                        r.setPatientName(fsPrescribe.getPatientName());
                                    }
                                }
                            }
                            r.setId(fsFollowReport.getId());
                            r.setFollowCount(fsFollowReport.getFollowCount()+1);
                            r.setCountTime(new Date());
                            fsFollowReportMapper.updateFsFollowReport(r);
                        }

                    }
                }
            }
        }
    }

    @Override
    public Boolean isEntityNull(FsFollowReportParam param) {
        if (param == null) {
            return true; // 整个实体对象为null
        }

        Class<? extends FsFollowReportParam> entityClass = param.getClass();
        Field[] fields = entityClass.getDeclaredFields();
        try {
            for (Field field : fields) {
                field.setAccessible(true);
                Object value = field.get(param);
                if (value != null) {
                    return false; // 实体对象中有字段不为null
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return true; // 实体对象内所有字段为null
    }

    @Override
    public Long selectFsFollowReportListVOCount(FsFollowReportParam fsFollowReport) {

       return fsFollowReportMapper.selectFsFollowReportListVOCount(fsFollowReport);
    }
}
