package com.fs.his.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.hutool.json.JSONUtil;
import com.fs.common.core.domain.R;
import com.fs.common.utils.DateUtils;
import com.fs.his.domain.*;
import com.fs.his.mapper.FsDoctorMapper;
import com.fs.his.mapper.FsPrescribeMapper;
import com.fs.his.mapper.FsStoreOrderMapper;
import com.fs.his.param.FsDrugReportCountParam;
import com.fs.his.vo.FsDrugReportCountListVO;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.his.mapper.FsDrugReportCountMapper;
import com.fs.his.service.IFsDrugReportCountService;

/**
 * 咨询统计Service业务层处理
 *
 * @author fs
 * @date 2024-04-23
 */
@Service
public class FsDrugReportCountServiceImpl implements IFsDrugReportCountService
{
    @Autowired
    private FsDrugReportCountMapper fsDrugReportCountMapper;

    @Autowired
    private FsStoreOrderMapper fsStoreOrderMapper;
    @Autowired
    private FsPrescribeMapper fsPrescribeMapper;
    @Autowired
    private FsDoctorMapper doctorMapper;

    /**
     * 查询咨询统计
     *
     * @param id 咨询统计主键
     * @return 咨询统计
     */
    @Override
    public FsDrugReportCount selectFsDrugReportCountById(Long id)
    {
        return fsDrugReportCountMapper.selectFsDrugReportCountById(id);
    }

    /**
     * 查询咨询统计列表
     *
     * @param fsDrugReportCount 咨询统计
     * @return 咨询统计
     */
    @Override
    public List<FsDrugReportCount> selectFsDrugReportCountList(FsDrugReportCount fsDrugReportCount)
    {
        return fsDrugReportCountMapper.selectFsDrugReportCountList(fsDrugReportCount);
    }

    /**
     * 新增咨询统计
     *
     * @param fsDrugReportCount 咨询统计
     * @return 结果
     */
    @Override
    public int insertFsDrugReportCount(FsDrugReportCount fsDrugReportCount)
    {
        return fsDrugReportCountMapper.insertFsDrugReportCount(fsDrugReportCount);
    }

    /**
     * 修改咨询统计
     *
     * @param fsDrugReportCount 咨询统计
     * @return 结果
     */
    @Override
    public int updateFsDrugReportCount(FsDrugReportCount fsDrugReportCount)
    {
        fsDrugReportCount.setUpdateTime(DateUtils.getNowDate());
        return fsDrugReportCountMapper.updateFsDrugReportCount(fsDrugReportCount);
    }

    /**
     * 批量删除咨询统计
     *
     * @param ids 需要删除的咨询统计主键
     * @return 结果
     */
    @Override
    public int deleteFsDrugReportCountByIds(Long[] ids)
    {
        return fsDrugReportCountMapper.deleteFsDrugReportCountByIds(ids);
    }

    /**
     * 删除咨询统计信息
     *
     * @param id 咨询统计主键
     * @return 结果
     */
    @Override
    public int deleteFsDrugReportCountById(Long id)
    {
        return fsDrugReportCountMapper.deleteFsDrugReportCountById(id);
    }

    @Override
    public R addReportCountByUserIdAndDoctorId(Long userId, Long doctorId) {
                List<FsStoreOrder> fsStoreOrders = fsStoreOrderMapper.selectFsStoreOrderByUserIdAndDoctorIdAndCompany(userId, doctorId);
                for (FsStoreOrder fsStoreOrder : fsStoreOrders) {
                    Long companyId = fsStoreOrder.getCompanyId();
                    String moth = new SimpleDateFormat("yyyyMM").format(new Date());
                    FsDrugReportCount fsDrugReportCount = fsDrugReportCountMapper.selectFsFollowReportByCompanyIdAndUseridAndDoctorId(userId, doctorId, companyId, moth);
                    if (fsDrugReportCount==null){
                        FsDrugReportCount r = new FsDrugReportCount();
                        r.setDoctorId(doctorId);
                        r.setUserId(userId);
                        r.setCompanyId(companyId);
                        r.setMonth(moth);
                        r.setCount(1L);
                        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(doctorId);
                        r.setDoctorName(fsDoctor.getDoctorName());
                        r.setDoctorAccount(fsDoctor.getAccount());
                        fsDrugReportCountMapper.insertFsDrugReportCount(r);
                    }else {
                        FsDrugReportCount r = new FsDrugReportCount();
                            if (fsStoreOrder.getPrescribeId()!=null){
                                FsPrescribe fsPrescribe = fsPrescribeMapper.selectFsPrescribeByPrescribeId(fsStoreOrder.getPrescribeId());
                                if (fsPrescribe!=null){
                                    if (fsDrugReportCount.getPatientName()!=null){
                                        if (!fsDrugReportCount.getPatientName().contains(fsPrescribe.getPatientName())){
                                            r.setPatientName(fsDrugReportCount.getPatientName()+","+fsPrescribe.getPatientName());
                                        }
                                    }else {
                                        r.setPatientName(fsPrescribe.getPatientName());
                                    }
                                }
                            }
                            r.setId(fsDrugReportCount.getId());
                            r.setCount(fsDrugReportCount.getCount()+1);
                            r.setUpdateTime(new Date());
                        fsDrugReportCountMapper.updateFsDrugReportCount(r);
                    }
                }



        return R.ok();
    }

    @Override
    public List<FsDrugReportCountListVO> selectFsDrugReportCountListVO(FsDrugReportCountParam fsDrugReportCount) {
        return fsDrugReportCountMapper.selectFsDrugReportCountListVO(fsDrugReportCount);
    }
}
