package com.fs.hisStore.service.impl;

import com.fs.common.utils.DateUtils;
import com.fs.hisStore.domain.FsFollowScrm;
import com.fs.hisStore.mapper.FsFollowScrmMapper;
import com.fs.hisStore.mapper.FsFollowTempScrmMapper;
import com.fs.hisStore.param.FsFollowListDParam;
import com.fs.hisStore.param.FsFollowListUParam;
import com.fs.hisStore.param.FsFollowParam;
import com.fs.hisStore.service.IFsFollowScrmService;
import com.fs.hisStore.vo.*;
import com.fs.system.mapper.SysConfigMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 随访管理Service业务层处理
 *
 * @author fs
 * @date 2023-07-14
 */
@Service
public class FsFollowScrmServiceImpl implements IFsFollowScrmService
{
    @Autowired
    private FsFollowScrmMapper fsFollowMapper;
    @Autowired
    private FsFollowTempScrmMapper fsFollowTempMapper;
//    @Autowired
//    private FsPackageOrderMapper fsPackageOrderMapper;
//    @Autowired
//    private IImService imService;
    @Autowired
    private SysConfigMapper sysConfigMapper;
    Logger logger= LoggerFactory.getLogger(getClass());
    /**
     * 查询随访管理
     *
     * @param followId 随访管理主键
     * @return 随访管理
     */
    @Override
    public FsFollowScrm selectFsFollowByFollowId(Long followId)
    {
        return fsFollowMapper.selectFsFollowByFollowId(followId);
    }

    /**
     * 查询随访管理列表
     *
     * @param fsFollow 随访管理
     * @return 随访管理
     */
    @Override
    public List<FsFollowScrm> selectFsFollowList(FsFollowScrm fsFollow)
    {
        return fsFollowMapper.selectFsFollowList(fsFollow);
    }

    /**
     * 新增随访管理
     *
     * @param fsFollow 随访管理
     * @return 结果
     */
    @Override
    public int insertFsFollow(FsFollowScrm fsFollow)
    {
        fsFollow.setCreateTime(DateUtils.getNowDate());
        return fsFollowMapper.insertFsFollow(fsFollow);
    }

    /**
     * 修改随访管理
     *
     * @param fsFollow 随访管理
     * @return 结果
     */
    @Override
    public int updateFsFollow(FsFollowScrm fsFollow)
    {
        fsFollow.setUpdateTime(DateUtils.getNowDate());
        return fsFollowMapper.updateFsFollow(fsFollow);
    }

    /**
     * 批量删除随访管理
     *
     * @param followIds 需要删除的随访管理主键
     * @return 结果
     */
    @Override
    public int deleteFsFollowByFollowIds(Long[] followIds)
    {
        return fsFollowMapper.deleteFsFollowByFollowIds(followIds);
    }

    /**
     * 删除随访管理信息
     *
     * @param followId 随访管理主键
     * @return 结果
     */
    @Override
    public int deleteFsFollowByFollowId(Long followId)
    {
        return fsFollowMapper.deleteFsFollowByFollowId(followId);
    }

    @Override
    public List<FsFollowListVO> selectFsFollowListVO(FsFollowParam fsFollow) {
        return fsFollowMapper.selectFsFollowListVO(fsFollow);
    }

    @Override
    public FsFollowVO selectFsFollowVOByFollowId(Long followId) {
        return fsFollowMapper.selectFsFollowVOByFollowId(followId);
    }

//    @Override
//    public void addFsFollowByPackageOrder(FsPackageOrder order) {
//        SysConfig sysConfig = sysConfigMapper.selectConfigByConfigKey("his.package");
//        Map<String, Object> config = (Map<String, Object>) JSON.parse(sysConfig.getConfigValue());
//        Integer followRate = (Integer)config.get("followRate");
//        FsFollowTemp fsFollowTemp = fsFollowTempMapper.selectFsFollowTempByTempId(order.getFollowTempId());
//        if (fsFollowTemp!=null){
//            FsFollow fsFollow = new FsFollow();
//            fsFollow.setTempId(order.getFollowTempId());
//            Map<String,Object> parse = (Map<String,Object>)JSON.parse(order.getPatientJson());
//            fsFollow.setPatientPhone((String) parse.get("mobile"));
//            fsFollow.setPatientName((String) parse.get("patientName"));
//            fsFollow.setPatientId((int)parse.get("patientId")+0L);
//            fsFollow.setUserId(order.getUserId());
//            fsFollow.setDoctorId(order.getFollowDoctorId());
//            fsFollow.setTotalNum(1);
//            fsFollow.setCompanyId(order.getCompanyId());
//            fsFollow.setCompanyUserId(order.getCompanyUserId());
//            fsFollow.setDeptId(order.getDeptId());
//            fsFollow.setNum(1);
//            fsFollow.setPackageOrderId(order.getOrderId());
//            fsFollow.setInquiryOrderId(order.getInquiryOrderId());
//            fsFollow.setPlanTime(DateUtils.getNowDate());
//            fsFollow.setPushStatus(1);
//            fsFollow.setWriteStatus(0);
//            fsFollow.setFormJson(fsFollowTemp.getFormJson());
//            fsFollow.setCreateTime(DateUtils.getNowDate());
//            int i = fsFollowMapper.insertFsFollow(fsFollow);
//            MsgDTO msgDTO=new MsgDTO();
//            MsgCustomDTO customDTO=new MsgCustomDTO();
//            customDTO.setType("follow");
//            customDTO.setOrderType(2);
//            customDTO.setFollowId(fsFollow.getFollowId().toString());
//            customDTO.setOrderId(fsFollow.getPackageOrderId().toString());
//            customDTO.setImType(2);
//            msgDTO.setCloudCustomData(JSONUtil.toJsonStr(customDTO));
//
//            msgDTO.setFrom_Account("D-"+fsFollow.getDoctorId());
//            msgDTO.setTo_Account("U-"+fsFollow.getUserId());
//            List<MsgDataDTO> msgs=new ArrayList<>();
//            MsgDataDTO msg=new MsgDataDTO();
//            String ext= JSONUtil.toJsonStr(fsFollow);
//            String followId=fsFollow.getFollowId().toString();
//            msg.setMsgContent(new MsgDataFormatDTO("follow",ext,followId));
//            msg.setMsgType("TIMCustomElem");//TIMCustomElem
//            msgs.add(msg);
//            msgDTO.setMsgBody(msgs);
//            MsgResponseDTO msgResponseDTO = imService.sendMsg(msgDTO);
//            logger.info("发送随访"+msgResponseDTO);
//
//            MsgDTO msgDTO2=new MsgDTO();
//            msgDTO2.setCloudCustomData(JSONUtil.toJsonStr(customDTO));
//
//            msgDTO2.setFrom_Account("D-"+fsFollow.getDoctorId());
//            msgDTO2.setTo_Account("U-"+fsFollow.getUserId());
//            List<MsgDataDTO> msgs2=new ArrayList<>();
//            MsgDataDTO msg2=new MsgDataDTO();
//            msg2.setMsgContent(new MsgDataFormatDTO("尊敬的用户，为了您的健康，请认真填写随访单，以便医生随时了解您的用药情况，评估治疗效果。\n" +
//                    "如果在用药中出现任何不适，可直接在对话框中描述您目前的情况，我将及时为您解答！"));
//            msg2.setMsgType("TIMTextElem");//TIMCustomElem
//            msgs2.add(msg2);
//            msgDTO2.setMsgBody(msgs2);
//
//            MsgResponseDTO msgResponseDTO2 = imService.sendMsg(msgDTO2);
//            logger.info("发送随访2"+msgResponseDTO2);
//            if (i>0){
//                FsPackageOrder fsPackageOrder = new FsPackageOrder();
//                fsPackageOrder.setOrderId(order.getOrderId());
//                Calendar calendar = Calendar.getInstance();
//                calendar.setTime(order.getFollowTime());
//                calendar.add(Calendar.DAY_OF_MONTH, followRate);
//                fsPackageOrder.setFollowTime(calendar.getTime());
//                fsPackageOrder.setFollowCount(order.getFollowCount()+1);
//                fsPackageOrderMapper.updateFsPackageOrder(fsPackageOrder);
//            }
//
//        }
//
//    }

    @Override
    public List<FsFollowListUVO> selectFsFollowListUVO(FsFollowListUParam param) {
        return fsFollowMapper.selectFsFollowListUVO(param);
    }

    @Override
    public List<FsFollowListDVO> selectFsFollowListDVO(FsFollowListDParam param) {
        return fsFollowMapper.selectFsFollowListDVO(param);
    }

    @Override
    public List<FsFollowExcelListVO> selectFsFollowListExcelVO(FsFollowParam fsFollow) {
        return fsFollowMapper.selectFsFollowListExcelVO(fsFollow);
    }

    @Override
    public List<FsFollowExportListVO> selectFsFollowExportListVO(FsFollowParam fsFollow) {
        return fsFollowMapper.selectFsFollowExportListVO(fsFollow);
    }
}
