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.hospital.mapper.*;
import com.fs.hospital.param.HospitalProductCheckOrderAddParam;
import com.fs.hospital.domain.*;
import com.fs.hospital.vo.HospitalProductCheckOrderHVO;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.hospital.service.IHospitalProductCheckOrderService;

/**
 * 采购单Service业务层处理
 *
 * @author fs
 * @date 2024-03-14
 */
@Service
public class HospitalProductCheckOrderServiceImpl implements IHospitalProductCheckOrderService
{
    @Autowired
    private HospitalProductCheckOrderMapper hospitalProductCheckOrderMapper;
    @Autowired
    private HospitalProductCheckOrderItemMapper hospitalProductCheckOrderItemMapper;
    @Autowired
    private HospitalProductLogsMapper hospitalProductLogsMapper;
    @Autowired
    private HospitalProductMapper hospitalProductMapper;
    @Autowired
    private HospitalProductPurchaseOrderItemMapper hospitalProductPurchaseOrderItemMapper;
    /**
     * 查询采购单
     *
     * @param orderId 采购单主键
     * @return 采购单
     */
    @Override
    public HospitalProductCheckOrder selectHospitalProductCheckOrderByOrderId(Long orderId)
    {
        return hospitalProductCheckOrderMapper.selectHospitalProductCheckOrderByOrderId(orderId);
    }

    /**
     * 查询采购单列表
     *
     * @param hospitalProductCheckOrder 采购单
     * @return 采购单
     */
    @Override
    public List<HospitalProductCheckOrder> selectHospitalProductCheckOrderList(HospitalProductCheckOrder hospitalProductCheckOrder)
    {


        return hospitalProductCheckOrderMapper.selectHospitalProductCheckOrderList(hospitalProductCheckOrder);
    }

    /**
     * 新增采购单
     *
     * @param
     * @return 结果
     */
    @Override
    public R insertHospitalProductCheckOrder(HospitalProductCheckOrderAddParam param)
    {
        HospitalProductCheckOrder order = new HospitalProductCheckOrder();
        String orderCode = param.getOrderCode()+ IdUtil.getSnowflake(0, 0).nextIdStr();
        param.setOrderCode(orderCode);
        BeanUtils.copyProperties(param,order);
        order.setCreateTime(DateUtils.getNowDate());
        Integer totalCount =0;
        Integer checkCount =0;
        BigDecimal buyMoney=new BigDecimal("0");
        BigDecimal sellMoney=new BigDecimal("0");
        hospitalProductCheckOrderMapper.insertHospitalProductCheckOrder(order);
        for (HospitalProductCheckOrderItem item : param.getItemList()) {
            HospitalProduct hospitalProduct = hospitalProductMapper.selectHospitalProductByProductId(item.getProductId());

            Integer count = item.getCount();
            totalCount+= count;
            int itemCheckCount = count-hospitalProduct.getTotalCount();
            checkCount += itemCheckCount;
            item.setCheckCount(count);
            item.setCount(Integer.parseInt(hospitalProduct.getTotalCount()+""));

            BigDecimal itemBuyMoney=new BigDecimal("0");
            if (hospitalProduct!=null){
                if (hospitalProduct.getTotalCount()<item.getCheckCount()){
                    HospitalProductPurchaseOrderItem hospitalProductPurchaseOrderItem = hospitalProductCheckOrderMapper.selectHospitalPurchaseOrderItemByProductIdOne(item.getProductId());
                    int i = item.getCheckCount() - hospitalProduct.getTotalCount();
                    hospitalProductPurchaseOrderItem.setLimitCount(hospitalProductPurchaseOrderItem.getLimitCount()+i);
                    itemBuyMoney=  itemBuyMoney.add(hospitalProductPurchaseOrderItem.getPrice().multiply(new BigDecimal(i)));
                    hospitalProductPurchaseOrderItemMapper.updateHospitalPurchaseOrderItem(hospitalProductPurchaseOrderItem);
                    item.setBatch(hospitalProductPurchaseOrderItem.getItemId());
                    item.setBatchNumber(hospitalProductPurchaseOrderItem.getBatchNumber());
                }else if (hospitalProduct.getTotalCount()>item.getCheckCount()){
                    List<HospitalProductPurchaseOrderItem> hospitalProductPurchaseOrderItems = hospitalProductCheckOrderMapper.selectHospitalPurchaseOrderItemByProductId(item.getProductId());
                    Integer check = hospitalProduct.getTotalCount()-item.getCheckCount();
                    for (HospitalProductPurchaseOrderItem hospitalProductPurchaseOrderItem : hospitalProductPurchaseOrderItems) {
                        if (hospitalProductPurchaseOrderItem.getLimitCount()>=check){
                            hospitalProductPurchaseOrderItem.setLimitCount(hospitalProductPurchaseOrderItem.getLimitCount()-check);
                            itemBuyMoney=  itemBuyMoney.add(hospitalProductPurchaseOrderItem.getPrice().multiply(new BigDecimal(check)).negate());
                            check=0;
                        }else {

                            check=check-hospitalProductPurchaseOrderItem.getLimitCount();
                            itemBuyMoney=  itemBuyMoney.add(hospitalProductPurchaseOrderItem.getPrice().multiply(new BigDecimal(check)).negate());
                            hospitalProductPurchaseOrderItem.setLimitCount(0);
                        }
                        hospitalProductPurchaseOrderItemMapper.updateHospitalPurchaseOrderItem(hospitalProductPurchaseOrderItem);
                        if (check==0){
                            break;
                        }
                    }
                }
                HospitalProduct p = new HospitalProduct();
                p.setProductId(item.getProductId());
                p.setTotalCount(item.getCheckCount());
                p.setSellCount(item.getCheckCount());
                hospitalProductMapper.updateHospitalProduct(p);
                buyMoney = buyMoney.add(itemBuyMoney);
                sellMoney = sellMoney.add(hospitalProduct.getSellPrice().multiply(new BigDecimal(itemCheckCount)));
                item.setBuyPrice(hospitalProduct.getBuyPrice());
                item.setBuyMoney(itemBuyMoney);
                item.setSellMoney(hospitalProduct.getSellPrice().multiply(new BigDecimal(itemCheckCount)));
                item.setLimitDate(hospitalProduct.getLimitDate());
                item.setOrderId(order.getOrderId());
                item.setCreateTime(DateUtils.getNowDate());
                hospitalProductCheckOrderItemMapper.insertHospitalProductCheckOrderItem(item);
            }
            order.setTotalCount(totalCount);
            order.setCheckCount(checkCount);
            order.setSellMoney(sellMoney);
            order.setBuyMoney(buyMoney);
        }

        hospitalProductCheckOrderMapper.updateHospitalProductCheckOrder(order);

        return R.ok();

    }

    /**
     * 修改采购单
     *
     * @param hospitalProductCheckOrder 采购单
     * @return 结果
     */
    @Override
    public int updateHospitalProductCheckOrder(HospitalProductCheckOrder hospitalProductCheckOrder)
    {
        return hospitalProductCheckOrderMapper.updateHospitalProductCheckOrder(hospitalProductCheckOrder);
    }

    /**
     * 批量删除采购单
     *
     * @param orderIds 需要删除的采购单主键
     * @return 结果
     */
    @Override
    public int deleteHospitalProductCheckOrderByOrderIds(Long[] orderIds)
    {
        return hospitalProductCheckOrderMapper.deleteHospitalProductCheckOrderByOrderIds(orderIds);
    }

    /**
     * 删除采购单信息
     *
     * @param orderId 采购单主键
     * @return 结果
     */
    @Override
    public int deleteHospitalProductCheckOrderByOrderId(Long orderId)
    {
        return hospitalProductCheckOrderMapper.deleteHospitalProductCheckOrderByOrderId(orderId);
    }

    @Override
    public HospitalProductCheckOrderHVO selectHospitalProductCheckOrderByOrderIdHVO(Long orderId) {
        return hospitalProductCheckOrderMapper.selectHospitalProductCheckOrderByOrderIdHVO(orderId);
    }
}
