package com.fs.watch.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fs.common.utils.DateUtils;
import com.fs.common.utils.StringUtils;
import com.fs.his.domain.FsPackage;
import com.fs.his.domain.FsStoreOrder;
import com.fs.his.dto.FsPackagePruductDTO;
import com.fs.his.service.IFsPackageOrderService;
import com.fs.watch.domain.WatchFsUser;
import com.fs.watch.domain.WatchMedicationTask;
import com.fs.watch.domain.vo.WatchUserAndMedicationVo;
import com.fs.watch.mapper.WatchMedicationTaskMapper;
import com.fs.watch.service.WatchMedicationTaskService;
import com.fs.watch.service.WatchUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

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

/**
 * 腕用药提醒Service业务层处理
 *
 * @author fs
 * @date 2024-12-10
 */
@Service
public class WatchMedicationTaskServiceImpl implements WatchMedicationTaskService {
    @Autowired
    private WatchMedicationTaskMapper watchMedicationTaskMapper;
    @Autowired
    private WatchUserService watchUserService;
    @Autowired
    @Lazy
    private IFsPackageOrderService fsPackageOrderService;
    @Autowired
    private WatchMedicationTaskService watchMedicationTaskService;

    /**
     * 查询腕用药提醒
     *
     * @param id 腕用药提醒主键
     * @return 腕用药提醒
     */
    @Override
    public WatchMedicationTask selectWatchMedicationTaskById(Long id) {
        return watchMedicationTaskMapper.selectWatchMedicationTaskById(id);
    }

    /**
     * 查询腕用药提醒列表
     *
     * @param watchMedicationTask 腕用药提醒
     * @return 腕用药提醒
     */
    @Override
    public List<WatchMedicationTask> selectWatchMedicationTaskList(WatchMedicationTask watchMedicationTask) {
        return watchMedicationTaskMapper.selectWatchMedicationTaskList(watchMedicationTask);
    }

    /**
     * 新增腕用药提醒
     *
     * @param watchMedicationTask 腕用药提醒
     * @return 结果
     */
    @Override
    public int insertWatchMedicationTask(WatchMedicationTask watchMedicationTask) {
        watchMedicationTask.setCreateTime(DateUtils.getNowDate());
        return watchMedicationTaskMapper.insertWatchMedicationTask(watchMedicationTask);
    }

    /**
     * 修改腕用药提醒
     *
     * @param watchMedicationTask 腕用药提醒
     * @return 结果
     */
    @Override
    public int updateWatchMedicationTask(WatchMedicationTask watchMedicationTask) {
        watchMedicationTask.setUpdateTime(DateUtils.getNowDate());
        return watchMedicationTaskMapper.updateWatchMedicationTask(watchMedicationTask);
    }

    /**
     * 批量删除腕用药提醒
     *
     * @param ids 需要删除的腕用药提醒主键
     * @return 结果
     */
    @Override
    public int deleteWatchMedicationTaskByIds(Long[] ids) {
        return watchMedicationTaskMapper.deleteWatchMedicationTaskByIds(ids);
    }

    /**
     * 删除腕用药提醒信息
     *
     * @param id 腕用药提醒主键
     * @return 结果
     */
    @Override
    public int deleteWatchMedicationTaskById(Long id) {
        return watchMedicationTaskMapper.deleteWatchMedicationTaskById(id);
    }

    /**
     * 批量添加
     *
     * @param tasks
     * @return
     */
    @Override
    public int insertList(ArrayList<WatchMedicationTask> tasks) {
        return watchMedicationTaskMapper.insertList(tasks);
    }

    @Override
    public List<WatchUserAndMedicationVo> medicationTaskList(Integer medicationTime) {
        return watchMedicationTaskMapper.medicationTaskList(medicationTime);
    }

    @Override
    public void addMedicationTask(FsStoreOrder order) {
        //2.新增用药提醒任务
        //2.1判断该用户是否已经绑腕表
        WatchFsUser watchFsUser = watchUserService.selectWatchFsUserById(order.getUserId());
        if (watchFsUser != null && StringUtils.isNotBlank(watchFsUser.getDeviceId())) {
            //已绑定则新增用药提醒任务
            //2.2判断是否包含套餐 ,只有套餐有用法
            if (order.getPackageOrderId() != null) {
                //查询是否含有改套餐订单以及套餐信息
                FsPackage fsPackage = fsPackageOrderService.selectFsPackageByOrderId(order.getPackageOrderId());
                if (fsPackage != null && StringUtils.isNotBlank(fsPackage.getProductJson())) {
                    List<FsPackagePruductDTO> fsPackageProductDTOS = JSONObject.parseArray(fsPackage.getProductJson(), FsPackagePruductDTO.class);
                    if (fsPackageProductDTOS != null && !fsPackageProductDTOS.isEmpty()) {
                        //添加任务
                        ArrayList<WatchMedicationTask> tasks = new ArrayList<>();
                        for (FsPackagePruductDTO dto : fsPackageProductDTOS) {
                            if (dto.getUsageFrequencyUnit() != null) {
                                WatchMedicationTask watchMedicationTask = new WatchMedicationTask();
                                watchMedicationTask.setPackageOrderId(order.getPackageOrderId());
                                watchMedicationTask.setPackageId(fsPackage.getPackageId());
                                watchMedicationTask.setProductId(dto.getProductId());
                                watchMedicationTask.setProductName(dto.getProductName());
                                watchMedicationTask.setUsageFrequencyUnit(dto.getUsageFrequencyUnit());
                                //默认提醒三天
                                watchMedicationTask.setRequiredNum(dto.getUsageFrequencyUnit() * 3);
                                watchMedicationTask.setActualNum(0);
                                watchMedicationTask.setCreateTime(new Date());
                                watchMedicationTask.setUserId(order.getUserId());
                                watchMedicationTask.setStatus(0);
                                tasks.add(watchMedicationTask);
                            }
                        }
                        insertList(tasks);
                    }
                }
            }
        }

    }
}
