package com.fs.his.service.impl;

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

import com.alibaba.fastjson.JSON;
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.domain.FsStoreOrderItem;
import com.fs.his.dto.FsPackagePruductDTO;
import com.fs.his.dto.FsStoreOrderItemDTO;
import com.fs.his.mapper.FsStoreOrderItemMapper;
import com.fs.his.service.IFsPackageOrderService;
import com.fs.hisStore.domain.FsStoreOrderScrm;
import com.fs.watch.domain.WatchFsUser;
import com.fs.watch.domain.WatchMedicationTask;
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.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.fs.his.mapper.FsUserWatchMapper;
import com.fs.his.domain.FsUserWatch;
import com.fs.his.service.IFsUserWatchService;

/**
 * 用户腕Service业务层处理
 *
 * @author fs
 * @date 2024-11-14
 */
@Service
public class FsUserWatchServiceImpl implements IFsUserWatchService
{
    @Autowired
    private FsUserWatchMapper fsUserWatchMapper;
    @Autowired
    private FsStoreOrderItemMapper fsStoreOrderItemMapper;
    @Autowired
    private WatchUserService watchUserService;
    @Autowired
    @Lazy
    private IFsPackageOrderService fsPackageOrderService;
    @Autowired
    private WatchMedicationTaskService watchMedicationTaskService;
    /**
     * 查询用户腕
     *
     * @param id 用户腕主键
     * @return 用户腕
     */
    @Override
    public FsUserWatch selectFsUserWatchById(Long id)
    {
        return fsUserWatchMapper.selectFsUserWatchById(id);
    }

    /**
     * 查询用户腕列表
     *
     * @param fsUserWatch 用户腕
     * @return 用户腕
     */
    @Override
    public List<FsUserWatch> selectFsUserWatchList(FsUserWatch fsUserWatch)
    {
        return fsUserWatchMapper.selectFsUserWatchList(fsUserWatch);
    }

    /**
     * 新增用户腕
     *
     * @param fsUserWatch 用户腕
     * @return 结果
     */
    @Override
    public int insertFsUserWatch(FsUserWatch fsUserWatch)
    {
        fsUserWatch.setCreateTime(DateUtils.getNowDate());
        return fsUserWatchMapper.insertFsUserWatch(fsUserWatch);
    }

    /**
     * 修改用户腕
     *
     * @param fsUserWatch 用户腕
     * @return 结果
     */
    @Override
    public int updateFsUserWatch(FsUserWatch fsUserWatch)
    {
        fsUserWatch.setUpdateTime(DateUtils.getNowDate());
        return fsUserWatchMapper.updateFsUserWatch(fsUserWatch);
    }

    /**
     * 批量删除用户腕
     *
     * @param ids 需要删除的用户腕主键
     * @return 结果
     */
    @Override
    public int deleteFsUserWatchByIds(Long[] ids)
    {
        return fsUserWatchMapper.deleteFsUserWatchByIds(ids);
    }

    /**
     * 删除用户腕信息
     *
     * @param id 用户腕主键
     * @return 结果
     */
    @Override
    public int deleteFsUserWatchById(Long id)
    {
        return fsUserWatchMapper.deleteFsUserWatchById(id);
    }

    @Async
    @Override
    public void addUserWatch(FsStoreOrder order) {

        List<FsStoreOrderItem> fsStoreOrderItems = fsStoreOrderItemMapper.selectFsStoreOrderItemListByOrderId(order.getOrderId());
        for (FsStoreOrderItem fsStoreOrderItem : fsStoreOrderItems) {
            String jsonInfo = fsStoreOrderItem.getJsonInfo();
            FsStoreOrderItemDTO fsStoreOrderItemDTO = JSON.parseObject(jsonInfo, FsStoreOrderItemDTO.class);
            //腕表条码
            if ("1014704".equals(fsStoreOrderItemDTO.getBarCode())){
                FsUserWatch fsUserWatch = new FsUserWatch();
                fsUserWatch.setUserId(order.getUserId());
                fsUserWatch.setCompanyId(order.getCompanyId());
                fsUserWatch.setCompanyUserId(order.getCompanyUserId());
                fsUserWatch.setDoctorId(order.getDoctorId());
                fsUserWatch.setStoreOrderId(order.getOrderId());
                fsUserWatch.setStatus(1);//已发货
                fsUserWatch.setCreateTime(DateUtils.getNowDate());
                fsUserWatchMapper.insertFsUserWatch(fsUserWatch);
            }

            //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);
                                }
                            }
                            watchMedicationTaskService.insertList(tasks);
                        }
                    }
                }
            }

        }
    }

    @Async
    @Override
    public void addUserWatchScrm(FsStoreOrderScrm order) {

        List<FsStoreOrderItem> fsStoreOrderItems = fsStoreOrderItemMapper.selectFsStoreOrderItemListByOrderId(order.getId());
        for (FsStoreOrderItem fsStoreOrderItem : fsStoreOrderItems) {
            String jsonInfo = fsStoreOrderItem.getJsonInfo();
            FsStoreOrderItemDTO fsStoreOrderItemDTO = JSON.parseObject(jsonInfo, FsStoreOrderItemDTO.class);
            //腕表条码
            if ("1014704".equals(fsStoreOrderItemDTO.getBarCode())){
                FsUserWatch fsUserWatch = new FsUserWatch();
                fsUserWatch.setUserId(order.getUserId());
                fsUserWatch.setCompanyId(order.getCompanyId());
                fsUserWatch.setCompanyUserId(order.getCompanyUserId());
                fsUserWatch.setDoctorId(order.getDoctorId());
                fsUserWatch.setStoreOrderId(order.getId());
                fsUserWatch.setStatus(1);//已发货
                fsUserWatch.setCreateTime(DateUtils.getNowDate());
                fsUserWatchMapper.insertFsUserWatch(fsUserWatch);
            }

            //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);
                                }
                            }
                            watchMedicationTaskService.insertList(tasks);
                        }
                    }
                }
            }

        }
    }

    /**
     * 根据userId查询最新得一条记录
     * @param userId 用户id
     * @return FsUserWatch
     */
    @Override
    public FsUserWatch getLastByUserId(Long userId) {
        return fsUserWatchMapper.getLastByUserId(userId);
    }
}
