package com.fs.hospital.service.impl;

import java.math.BigDecimal;
import java.util.List;

import cn.hutool.core.util.IdUtil;
import com.fs.common.core.domain.R;
import com.fs.common.utils.DateUtils;
import com.fs.common.utils.StringUtils;
import com.fs.core.utils.OrderCodeUtils;
import com.fs.hospital.domain.*;
import com.fs.hospital.mapper.*;
import com.fs.hospital.param.HospitalProductOutOrderAddParam;
import com.fs.hospital.vo.HospitalProductOutOrderHVO;
import com.fs.hospital.vo.HospitalProductOutOrderListHVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.hospital.service.IHospitalProductOutOrderService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 领用单Service业务层处理
 *
 * @author fs
 * @date 2024-03-14
 */
@Service
public class HospitalProductOutOrderServiceImpl implements IHospitalProductOutOrderService
{
    @Autowired
    private HospitalProductOutOrderMapper hospitalProductOutOrderMapper;
    @Autowired
    private HospitalProductOutOrderItemMapper hospitalProductOutOrderItemMapper;
    @Autowired
    private HospitalProductLogsMapper hospitalProductLogsMapper;
    @Autowired
    private HospitalProductMapper hospitalProductMapper;
    @Autowired
    private HospitalProductPurchaseOrderItemMapper hospitalProductPurchaseOrderItemMapper;
    /**
     * 查询领用单
     *
     * @param orderId 领用单主键
     * @return 领用单
     */
    @Override
    public HospitalProductOutOrder selectHospitalProductOutOrderByOrderId(Long orderId)
    {
        return hospitalProductOutOrderMapper.selectHospitalProductOutOrderByOrderId(orderId);
    }

    /**
     * 查询领用单列表
     *
     * @param hospitalProductOutOrder 领用单
     * @return 领用单
     */
    @Override
    public List<HospitalProductOutOrder> selectHospitalProductOutOrderList(HospitalProductOutOrder hospitalProductOutOrder)
    {
        return hospitalProductOutOrderMapper.selectHospitalProductOutOrderList(hospitalProductOutOrder);
    }

    /**
     * 新增领用单
     *
     * @param hospitalProductOutOrder 领用单
     * @return 结果
     */
    @Override
    @Transactional
    public R insertHospitalProductOutOrder(HospitalProductOutOrderAddParam param) {
        HospitalProductOutOrder order = new HospitalProductOutOrder();
        String orderCode = param.getOrderCode()+ IdUtil.getSnowflake(0, 0).nextIdStr();
        param.setOrderCode(orderCode);
        BeanUtils.copyProperties(param,order);
        order.setCreateTime(DateUtils.getNowDate());
        Integer totalCount =0;
        BigDecimal money=new BigDecimal("0");
        for (HospitalProductOutOrderItem hospitalProductOutOrderItem : param.getItemList()) {
            HospitalProduct hospitalProduct = hospitalProductMapper.selectHospitalProductByProductId(hospitalProductOutOrderItem.getProductId());
            if (hospitalProduct!=null){
                Integer count = hospitalProductOutOrderItem.getCount();
                totalCount+= count;
                BigDecimal m = hospitalProduct.getSellPrice();
                hospitalProductOutOrderItem.setMoney(hospitalProduct.getSellPrice());
                hospitalProductOutOrderItem.setLimitDate(hospitalProduct.getLimitDate());
                BigDecimal multiply = new BigDecimal(count).multiply(m);
                money = money.add(multiply);
            }

        }
        order.setTotalCount(totalCount);
        order.setMoney(money);
        if (hospitalProductOutOrderMapper.insertHospitalProductOutOrder(order)>0){
            for (HospitalProductOutOrderItem item : param.getItemList()){
                item.setHospitalId(order.getHospitalId());
                item.setOrderId(order.getOrderId());
                Integer itemCount = item.getCount();
                Integer count = item.getCount();

                List<HospitalProductPurchaseOrderItem> hospitalProductPurchaseOrderItems = hospitalProductPurchaseOrderItemMapper.selectHospitalPurchaseOrderItemByProductId(item.getProductId());
                for (HospitalProductPurchaseOrderItem hospitalProductPurchaseOrderItem : hospitalProductPurchaseOrderItems) {
                    if (hospitalProductPurchaseOrderItem.getLimitCount()>=count){
                        hospitalProductPurchaseOrderItem.setLimitCount(hospitalProductPurchaseOrderItem.getLimitCount()-count);
                        item.setCount(count);
                        count=0;
                    }else {
                        item.setCount(hospitalProductPurchaseOrderItem.getLimitCount());
                        count=count-hospitalProductPurchaseOrderItem.getLimitCount();
                        hospitalProductPurchaseOrderItem.setLimitCount(0);
                    }

                    hospitalProductPurchaseOrderItemMapper.updateHospitalPurchaseOrderItem(hospitalProductPurchaseOrderItem);
                    item.setBatch(hospitalProductPurchaseOrderItem.getItemId());
                    item.setBatchNumber(hospitalProductPurchaseOrderItem.getBatchNumber());
                    item.setLimitCount(item.getCount());
                    hospitalProductOutOrderItemMapper.insertHospitalProductOutOrderItem(item);
                    if (count==0){
                        break;
                    }
                }


                HospitalProduct p = hospitalProductMapper.selectHospitalProductByProductId(item.getProductId());
                HospitalProduct hospitalProduct = new HospitalProduct();
                hospitalProduct.setProductId(item.getProductId());
                hospitalProduct.setTotalCount(p.getTotalCount()-itemCount);
                hospitalProduct.setSellCount(p.getSellCount()-itemCount);
                hospitalProductMapper.updateHospitalProduct(hospitalProduct);

                //记录日志
                HospitalProductLogs logs = new HospitalProductLogs();
                logs.setHospitalId(param.getHospitalId());
                logs.setLogsType(2);
                logs.setBusinessId(order.getOrderId().toString());
                logs.setCreateTime(DateUtils.getNowDate());
                logs.setUserId(param.getUserId());
                logs.setRemark("领用");
                hospitalProductLogsMapper.insertHospitalProductLogs(logs);
            }

            return R.ok();
        }
        return R.error("新增失败");
    }

    @Override
    public R insertReturnHospitalProductOutOrder(HospitalProductOutOrderAddParam param) {
        HospitalProductOutOrder order = new HospitalProductOutOrder();
        String orderCode =  OrderCodeUtils.getOrderSn();
        if(StringUtils.isEmpty(orderCode)){
            return R.error("订单生成失败，请重试");
        }
        param.setOrderCode(orderCode);
        BeanUtils.copyProperties(param,order);
        order.setCreateTime(DateUtils.getNowDate());
        Integer totalCount =0;
        BigDecimal money=new BigDecimal("0");
        for (HospitalProductOutOrderItem hospitalProductOutOrderItem : param.getItemList()) {
            HospitalProduct hospitalProduct = hospitalProductMapper.selectHospitalProductByProductId(hospitalProductOutOrderItem.getProductId());
            if (hospitalProduct!=null){
                Integer count = hospitalProductOutOrderItem.getCount();
                totalCount+= count;
                BigDecimal m = hospitalProduct.getSellPrice();
                hospitalProductOutOrderItem.setMoney(hospitalProduct.getSellPrice());
                hospitalProductOutOrderItem.setLimitDate(hospitalProduct.getLimitDate());
                BigDecimal multiply = new BigDecimal(count).multiply(m);
                money = money.add(multiply);
            }

        }
        order.setTotalCount(totalCount);
        order.setMoney(money);
        if (hospitalProductOutOrderMapper.insertHospitalProductOutOrder(order)>0){
            for (HospitalProductOutOrderItem item : param.getItemList()){
                HospitalProductOutOrderItem it = new HospitalProductOutOrderItem();
                it.setItemId(item.getItemId());
                it.setLimitCount(item.getLimitCount()-item.getCount());
                hospitalProductOutOrderItemMapper.updateHospitalProductOutOrderItem(it);
                item.setHospitalId(order.getHospitalId());
                item.setOrderId(order.getOrderId());
                item.setLimitCount(0);
                Integer itemCount = item.getCount();
                item.setItemId(null);
                HospitalProductPurchaseOrderItem hospitalProductPurchaseOrderItem = hospitalProductPurchaseOrderItemMapper.selectHospitalPurchaseOrderItemByItemId(item.getBatch());
                hospitalProductPurchaseOrderItem.setLimitCount(hospitalProductPurchaseOrderItem.getLimitCount()+itemCount);
                hospitalProductPurchaseOrderItemMapper.updateHospitalPurchaseOrderItem(hospitalProductPurchaseOrderItem);

                hospitalProductOutOrderItemMapper.insertHospitalProductOutOrderItem(item);

                HospitalProduct p = hospitalProductMapper.selectHospitalProductByProductId(item.getProductId());
                HospitalProduct hospitalProduct = new HospitalProduct();
                hospitalProduct.setProductId(item.getProductId());
                hospitalProduct.setTotalCount(p.getTotalCount()+itemCount);
                hospitalProduct.setSellCount(p.getSellCount()+itemCount);
                hospitalProductMapper.updateHospitalProduct(hospitalProduct);

            }

            return R.ok();
        }
        return null;
    }

    @Override
    public List<HospitalProductOutOrderListHVO> selectHospitalProductOutOrderOutListPVO(HospitalProductOutOrder hospitalProductOutOrder) {
        return hospitalProductOutOrderMapper.selectHospitalProductOutOrderOutListPVO(hospitalProductOutOrder);
    }

    /**
     * 修改领用单
     *
     * @param hospitalProductOutOrder 领用单
     * @return 结果
     */
    @Override
    public int updateHospitalProductOutOrder(HospitalProductOutOrder hospitalProductOutOrder)
    {
        return hospitalProductOutOrderMapper.updateHospitalProductOutOrder(hospitalProductOutOrder);
    }

    /**
     * 批量删除领用单
     *
     * @param orderIds 需要删除的领用单主键
     * @return 结果
     */
    @Override
    public int deleteHospitalProductOutOrderByOrderIds(Long[] orderIds)
    {
        return hospitalProductOutOrderMapper.deleteHospitalProductOutOrderByOrderIds(orderIds);
    }

    /**
     * 删除领用单信息
     *
     * @param orderId 领用单主键
     * @return 结果
     */
    @Override
    public int deleteHospitalProductOutOrderByOrderId(Long orderId)
    {
        return hospitalProductOutOrderMapper.deleteHospitalProductOutOrderByOrderId(orderId);
    }

    @Override
    public List<HospitalProductOutOrderListHVO> selectHospitalProductOutOrderListPVO(HospitalProductOutOrder hospitalProductOutOrder) {
        return hospitalProductOutOrderMapper.selectHospitalProductOutOrderListPVO(hospitalProductOutOrder);
    }

    @Override
    public HospitalProductOutOrderHVO selectHospitalProductOutOrderByOrderIdHVO(Long orderId) {
        return hospitalProductOutOrderMapper.selectHospitalProductOutOrderByOrderIdHVO(orderId);
    }


}
