package com.fs.survey.service.impl;

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

import com.fs.audit.exception.BusinessException;
import com.fs.common.config.LoginContextManager;
import com.fs.common.utils.DateUtils;
import com.fs.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.survey.mapper.DrugResearchMapper;
import com.fs.survey.domain.DrugResearch;
import com.fs.survey.service.IDrugResearchService;

/**
 * 用药调研Service业务层处理
 * 
 * @author fs
 * @date 2026-02-06
 */
@Service
public class DrugResearchServiceImpl implements IDrugResearchService 
{
    @Autowired
    private DrugResearchMapper drugResearchMapper;

    /**
     * 查询用药调研
     * 
     * @param id 用药调研主键
     * @return 用药调研
     */
    @Override
    public DrugResearch selectDrugResearchById(Long id)
    {
        return drugResearchMapper.selectDrugResearchById(id);
    }

    /**
     * 查询用药调研列表
     * 
     * @param drugResearch 用药调研
     * @return 用药调研
     */
    @Override
    public List<DrugResearch> selectDrugResearchList(DrugResearch drugResearch)
    {
        // 处理时间范围
        if (drugResearch.getStartTime() != null) {
            drugResearch.setStartTime(DateUtils.atStartOfDay(drugResearch.getStartTime()));
        }
        if (drugResearch.getEndTime() != null) {
            drugResearch.setEndTime(DateUtils.atEndOfDay(drugResearch.getEndTime()));
        }

        // 校验时间顺序
        Date start = drugResearch.getStartTime();
        Date end = drugResearch.getEndTime();
        if (start != null && end != null && end.before(start)) {
            throw new BusinessException("结束时间必须大于开始时间");
        }
        drugResearch.setCreateTime(DateUtils.getNowDate());
        return drugResearchMapper.selectDrugResearchList(drugResearch);
    }

    /**
     * 新增用药调研
     * 
     * @param drugResearch 用药调研
     * @return 结果
     */
    @Override
    public int insertDrugResearch(DrugResearch drugResearch)
    {
        Long userId = SecurityUtils.getUserId();
        drugResearch.setCreateBy(userId);
        drugResearch.setUpdateBy(userId);
        drugResearch.setCreateTime(new Date());
        drugResearch.setUpdateTime(new Date());
        drugResearch.setDeleted(0L);
        return drugResearchMapper.insertDrugResearch(drugResearch);
    }

    @Override
    public int companyInsertDrugResearch(DrugResearch drugResearch) {
        Long userId = LoginContextManager.getCurrentCompanyUserId();
        drugResearch.setCreateBy(userId);
        drugResearch.setUpdateBy(userId);
        drugResearch.setCreateTime(new Date());
        drugResearch.setUpdateTime(new Date());
        drugResearch.setDeleted(0L);
        return drugResearchMapper.insertDrugResearch(drugResearch);
    }

    /**
     * 修改用药调研
     * 
     * @param drugResearch 用药调研
     * @return 结果
     */
    @Override
    public int updateDrugResearch(DrugResearch drugResearch)
    {
        drugResearch.setUpdateTime(DateUtils.getNowDate());
        return drugResearchMapper.updateDrugResearch(drugResearch);
    }

    /**
     * 批量删除用药调研
     * 
     * @param ids 需要删除的用药调研主键
     * @return 结果
     */
    @Override
    public int deleteDrugResearchByIds(Long[] ids)
    {
        return drugResearchMapper.deleteDrugResearchByIds(ids);
    }

    /**
     * 删除用药调研信息
     * 
     * @param id 用药调研主键
     * @return 结果
     */
    @Override
    public int deleteDrugResearchById(Long id)
    {
        return drugResearchMapper.deleteDrugResearchById(id);
    }
}
