package com.fs.hospital.service.impl;

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

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.fs.common.core.domain.R;
import com.fs.common.utils.DateUtils;
import com.fs.hospital.domain.*;
import com.fs.hospital.mapper.*;
import com.fs.hospital.param.*;
import com.fs.hospital.vo.HospitalOrderItemListHVO;
import com.fs.hospital.vo.HospitalTreatOrderHVO;
import com.fs.hospital.vo.HospitalTreatOrderListHVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.hospital.service.IHospitalTreatOrderService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 治疗单Service业务层处理
 *
 * @author fs
 * @date 2024-03-22
 */
@Service
public class HospitalTreatOrderServiceImpl implements IHospitalTreatOrderService
{
    @Autowired
    private HospitalTreatOrderMapper hospitalTreatOrderMapper;
    @Autowired
    private HospitalTreatOrderLogsMapper hospitalTreatOrderLogsMapper;

    @Autowired
    private HospitalPatientMapper hospitalPatientMapper;

    @Autowired
    private HospitalOrderMapper hospitalOrderMapper;

    @Autowired
    private HospitalOrderItemMapper hospitalOrderItemMapper;

    @Autowired
    HospitalOutpatientOrderServiceImpl  hospitalOutpatientOrderService;

    @Autowired
    private HospitalOrderItemLogsServiceImpl hospitalOrderItemLogsService;

    /**
     * 查询治疗单
     *
     * @param orderId 治疗单主键
     * @return 治疗单
     */
    @Override
    public HospitalTreatOrder selectHospitalTreatOrderByOrderId(Long orderId)
    {
        return hospitalTreatOrderMapper.selectHospitalTreatOrderByOrderId(orderId);
    }

    /**
     * 查询治疗单列表
     *
     * @param hospitalTreatOrder 治疗单
     * @return 治疗单
     */
    @Override
    public List<HospitalTreatOrder> selectHospitalTreatOrderList(HospitalTreatOrder hospitalTreatOrder)
    {
        return hospitalTreatOrderMapper.selectHospitalTreatOrderList(hospitalTreatOrder);
    }

    /**
     * 新增治疗单
     *
     * @param hospitalTreatOrder 治疗单
     * @return 结果
     */
    @Override
    public R insertHospitalTreatOrder(HospitalTreatOrder hospitalTreatOrder)
    {
        hospitalTreatOrder.setOrderCode("ZX"+ IdUtil.getSnowflake(0, 0).nextIdStr());
        if (hospitalTreatOrderMapper.insertHospitalTreatOrder(hospitalTreatOrder)>0) {
            return R.ok();
        }
//        HospitalTreatOrderLogs logs = new HospitalTreatOrderLogs();
//        logs.setOrderId(hospitalTreatOrder.getOrderId());
//        logs.setHospitalId(hospitalTreatOrder.getHospitalId());
//        logs.setUserId(hospitalTreatOrder.getUserId());
//        logs.setContent("开单");
//        logs.setCreateTime(DateUtils.getNowDate());
//        hospitalTreatOrderLogsMapper.insertHospitalTreatOrderLogs(logs);

        return R.error("开单失败");
    }

    /**
     * 修改治疗单
     *
     * @param hospitalTreatOrder 治疗单
     * @return 结果
     */
    @Override
    public int updateHospitalTreatOrder(HospitalTreatOrder hospitalTreatOrder)
    {
        hospitalTreatOrder.setUpdateTime(DateUtils.getNowDate());
        return hospitalTreatOrderMapper.updateHospitalTreatOrder(hospitalTreatOrder);
    }

    /**
     * 批量删除治疗单
     *
     * @param orderIds 需要删除的治疗单主键
     * @return 结果
     */
    @Override
    public int deleteHospitalTreatOrderByOrderIds(Long[] orderIds)
    {
        return hospitalTreatOrderMapper.deleteHospitalTreatOrderByOrderIds(orderIds);
    }

    /**
     * 删除治疗单信息
     *
     * @param orderId 治疗单主键
     * @return 结果
     */
    @Override
    public int deleteHospitalTreatOrderByOrderId(Long orderId)
    {
        return hospitalTreatOrderMapper.deleteHospitalTreatOrderByOrderId(orderId);
    }

    @Override
    public List<HospitalTreatOrderListHVO> selectHospitalTreatOrderListHVO(HospitalTreatOrderHParam hospitalTreatOrder) {
        return hospitalTreatOrderMapper.selectHospitalTreatOrderListHVO(hospitalTreatOrder);
    }


    /**
     *  完成开单
     */
    @Override
    @Transactional
    public R finishTreatOrder(HospitalTreatOrderAddParam param) {
        HospitalTreatOrder treatOrder = hospitalTreatOrderMapper.selectHospitalTreatOrderByOrderId(param.getOrderId());
        if (treatOrder!=null){
            if (treatOrder.getPatientId()==null){
                treatOrder.setPatientId(param.getPatientId());
                HospitalPatient patient = hospitalPatientMapper.selectHospitalPatientByPatientId(param.getPatientId());
                if (patient!=null){
                    treatOrder.setPatientJson(JSON.toJSONString(patient));
                }
            }
            treatOrder.setUserId(param.getUserId());
            treatOrder.setCreateTime(DateUtils.getNowDate());
            treatOrder.setTitle(param.getTitle());
            treatOrder.setOrderType(2);
            treatOrder.setStatus(2);
            treatOrder.setIsPay(0);
            treatOrder.setUpdateTime(new Date());
            treatOrder.setMoney(param.getMoney());
            List<HospitalOrderItemListHVO> list = param.getOrderItems();
            if (hospitalTreatOrderMapper.updateHospitalTreatOrder(treatOrder)>0){
                HospitalOrder hospitalOrder = new HospitalOrder();
                hospitalOrder.setOrderCode(IdUtil.getSnowflake(0, 0).nextIdStr());
                hospitalOrder.setHospitalId(treatOrder.getHospitalId());
                hospitalOrder.setTreatOrderId(treatOrder.getOrderId());
                hospitalOrder.setPatientId(treatOrder.getPatientId());
                hospitalOrder.setPatientJson(treatOrder.getPatientJson());
                hospitalOrder.setOrderType(3);
                hospitalOrder.setUserId(param.getUserId());
                hospitalOrder.setStatus(1);
                hospitalOrder.setCreateTime(new Date());
                hospitalOrder.setTotalMoney(param.getMoney());
                hospitalOrderMapper.insertHospitalOrder(hospitalOrder);
                if (list!=null&&list.size()>0){
                    //遍历orderItem生成订单详情
                    for (HospitalOrderItemListHVO item : list) {
                        HospitalOrderItem hospitalOrderItem = new HospitalOrderItem();
                        hospitalOrderItem.setOrderId(hospitalOrder.getOrderId());
                        if (treatOrder.getOrderId()!=null){
                            hospitalOrderItem.setTreatOrderId(treatOrder.getOrderId());
                        }
                        //记录剩余执行数量
                        if (item.getType()==3){
                            hospitalOrderItem.setTreatStatus(0);
                            hospitalOrderItem.setRemainCount(item.getCount());
                        }
                        hospitalOrderItem.setHospitalId(hospitalOrder.getHospitalId());
                        hospitalOrderItem.setItemType(item.getType());
                        hospitalOrderItem.setBusinessId(item.getId());
                        //判断类型存项目json
                        hospitalOutpatientOrderService.processItem(hospitalOrderItem, item);
                        hospitalOrderItem.setCount(item.getCount());
                        hospitalOrderItem.setPrice(item.getPrice());
                        hospitalOrderItem.setPayStatus(1);
                        hospitalOrderItemMapper.insertHospitalOrderItem(hospitalOrderItem);
                    }
                }
                return R.ok();
            }
        }
        return R.error();
    }

    @Override
    public List<HospitalOrderItemListHVO> selectOrderItemByTreatId(Long orderId) {
        List<HospitalOrderItem> orderItems = hospitalOrderItemMapper.selectHospitalOrderItemByTreatId(orderId);
        List<HospitalOrderItemListHVO> list = hospitalOutpatientOrderService.getOrderItemList(orderItems);
        return list;
    }

    @Override
    public List<HospitalOrderItemListHVO> selectOrderItemByExecute(Long orderId) {
        List<HospitalOrderItem> orderItems = hospitalOrderItemMapper.selectHospitalOrderItemByExecute(orderId);
        List<HospitalOrderItemListHVO> list = hospitalOutpatientOrderService.getOrderItemList(orderItems);
        return list;
    }

    @Override
    @Transactional
    public R itemExecute(HospitalTreatOrderExecuteParam param) {
        List<HospitalOrderItemExecuteParam>  itemExecuteParams = param.getParams();
        HospitalOrderItemLogs hospitalOrderItemLogs=new HospitalOrderItemLogs();
        StringBuilder stringBuilder=new StringBuilder();
        for (HospitalOrderItemExecuteParam itemExecuteParam : itemExecuteParams) {


            HospitalOrderItem hospitalOrderItem = hospitalOrderItemMapper.selectHospitalOrderItemByItemId(itemExecuteParam.getItemId());
            if (hospitalOrderItem != null) {
                hospitalOrderItem.setRemainCount(hospitalOrderItem.getRemainCount()-itemExecuteParam.getThisTimeExecuteCount());
                if (hospitalOrderItem.getRemainCount()<0){
                    return R.error("执行数量过多");
                }
                if (hospitalOrderItem.getRemainCount()==0){
                    hospitalOrderItem.setTreatStatus(1);//剩余数量为0变状态
                }
                hospitalOrderItemMapper.updateHospitalOrderItem(hospitalOrderItem);
                //记录执行日志
                stringBuilder.append(itemExecuteParam.getName()).append("(").append(itemExecuteParam.getThisTimeExecuteCount()+"次),");
            }
        }
        hospitalOrderItemLogs.setTreatOrderId(param.getOrderId());
        hospitalOrderItemLogs.setUserId(param.getUserId());
        hospitalOrderItemLogs.setExecuteItem(stringBuilder.toString());
        hospitalOrderItemLogsService.insertHospitalOrderItemLogs(hospitalOrderItemLogs);


        HospitalTreatOrder treatOrder = hospitalTreatOrderMapper.selectHospitalTreatOrderByOrderId(param.getOrderId());
        if (treatOrder!=null){
            Integer itemCount = hospitalOrderItemMapper.selectHospitalOrderItemIsExecute(treatOrder.getOrderId());;
            if (itemCount==0){
                treatOrder.setStatus(3);//全部执行完变状态
                this.updateHospitalTreatOrder(treatOrder);
            }
        }
        return R.ok();
    }


    @Override
    public HospitalTreatOrderHVO selectHospitalTreatOrderHVOByOrderId(Long orderId) {
        return hospitalTreatOrderMapper.selectHospitalTreatOrderHVOByOrderId(orderId);
    }
}
