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.domain.HospitalProduct;
import com.fs.hospital.domain.HospitalProductLogs;
import com.fs.hospital.domain.HospitalProductPurchaseOrderItem;
import com.fs.hospital.mapper.HospitalProductLogsMapper;
import com.fs.hospital.mapper.HospitalProductMapper;
import com.fs.hospital.mapper.HospitalProductPurchaseOrderItemMapper;
import com.fs.hospital.param.HospitalProductPurchaseOrderAddEditParam;
import com.fs.hospital.param.HospitalProductPurchaseOrderParam;
import com.fs.hospital.vo.HospitalProductPurchaseOrderHVO;
import com.fs.hospital.vo.HospitalProductPurchaseOrdeHVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.hospital.mapper.HospitalProductPurchaseOrderMapper;
import com.fs.hospital.domain.HospitalProductPurchaseOrder;
import com.fs.hospital.service.IHospitalProductPurchaseOrderService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 采购单Service业务层处理
 *
 * @author fs
 * @date 2024-03-12
 */
@Service
public class HospitalProductPurchaseOrderServiceImpl implements IHospitalProductPurchaseOrderService
{
    @Autowired
    private HospitalProductPurchaseOrderMapper hospitalProductPurchaseOrderMapper;
    @Autowired
    private HospitalProductPurchaseOrderItemMapper itemMapper;
    @Autowired
    private HospitalProductMapper hospitalProductMapper;
    @Autowired
    private HospitalProductLogsMapper productLogsMapper;


    /**
     * 查询采购单
     *
     * @param orderId 采购单主键
     * @return 采购单
     */
    @Override
    public HospitalProductPurchaseOrder selectHospitalPurchaseOrderByOrderId(Long orderId)
    {
        return hospitalProductPurchaseOrderMapper.selectHospitalPurchaseOrderByOrderId(orderId);
    }

    /**
     * 查询采购单列表
     *
     * @param hospitalProductPurchaseOrder 采购单
     * @return 采购单
     */
    @Override
    public List<HospitalProductPurchaseOrder> selectHospitalPurchaseOrderList(HospitalProductPurchaseOrder hospitalProductPurchaseOrder)
    {
        return hospitalProductPurchaseOrderMapper.selectHospitalPurchaseOrderList(hospitalProductPurchaseOrder);
    }

    /**
     * 新增采购单
     *

     * @return 结果
     */
    @Override
    @Transactional
    public R insertHospitalPurchaseOrder(HospitalProductPurchaseOrderAddEditParam param)
    {
        HospitalProductPurchaseOrder order = new HospitalProductPurchaseOrder();
        String orderCode = "RT"+IdUtil.getSnowflake(0, 0).nextIdStr();
        param.setOrderCode(orderCode);
        BeanUtils.copyProperties(param,order);
        order.setCreateTime(DateUtils.getNowDate());
        if (hospitalProductPurchaseOrderMapper.insertHospitalPurchaseOrder(order)>0){
            for (HospitalProductPurchaseOrderItem item : param.getItemList()){
                //新增item
                item.setHospitalId(order.getHospitalId());
                item.setOrderId(order.getOrderId());
                item.setLimitCount(item.getCount());
                item.setOrderType(1);
                if (itemMapper.insertHospitalPurchaseOrderItem(item)>0){
                    //修改商品信息
                    HospitalProduct product = hospitalProductMapper.selectHospitalProductByProductId(item.getProductId());
                    product.setBuyPrice(item.getPrice());
                    if (item.getLimitDate() != null){
                        product.setLimitDate(item.getLimitDate());
                    }
                    BigDecimal grossProfit = product.getSellPrice().subtract(product.getBuyPrice());
                    BigDecimal grossProfitMargin = grossProfit.divide(product.getSellPrice(),4,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100));
                    product.setGrossRate(grossProfitMargin);
                    product.setTotalCount(product.getTotalCount()+item.getCount());
                    product.setSellCount(product.getSellCount()+item.getCount());
                    BigDecimal addition = item.getPrice().multiply(new BigDecimal(item.getCount()));
                    product.setCostPrice(product.getCostPrice().add(addition));
                    product.setUpdateTime(DateUtils.getNowDate());
                    hospitalProductMapper.updateHospitalProduct(product);
                    //记录日志
                    HospitalProductLogs logs = new HospitalProductLogs();
                    logs.setHospitalId(param.getHospitalId());
                    logs.setLogsType(1);
                    logs.setBusinessId(order.getOrderId().toString());
                    logs.setCreateTime(DateUtils.getNowDate());
                    logs.setUserId(param.getUserId());
                    logs.setRemark("采购入库");
                    productLogsMapper.insertHospitalProductLogs(logs);
                }
            }

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

    /**
     * 修改采购单
     *
     * @param hospitalProductPurchaseOrder 采购单
     * @return 结果
     */
    @Override
    public int updateHospitalPurchaseOrder(HospitalProductPurchaseOrder hospitalProductPurchaseOrder)
    {
        return hospitalProductPurchaseOrderMapper.updateHospitalPurchaseOrder(hospitalProductPurchaseOrder);
    }

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

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

    @Override
    public List<HospitalProductPurchaseOrdeHVO> selectHospitalPurchaseOrderListPVO(HospitalProductPurchaseOrderParam hospitalPurchaseOrder) {
        return hospitalProductPurchaseOrderMapper.selectHospitalPurchaseOrderListPVO(hospitalPurchaseOrder);
    }

    @Override
    public HospitalProductPurchaseOrderHVO selectHospitalPurchaseOrderHVOByOrderId(Long orderId) {
        return hospitalProductPurchaseOrderMapper.selectHospitalPurchaseOrderHVOByOrderId(orderId);
    }

    @Override
    @Transactional
    public R addSalesOut(HospitalProductPurchaseOrderAddEditParam param) {
        HospitalProductPurchaseOrder order = new HospitalProductPurchaseOrder();
        String orderCode = "TH"+IdUtil.getSnowflake(0, 0).nextIdStr();
        param.setOrderCode(orderCode);
        param.setOrderType(2);
        BeanUtils.copyProperties(param,order);
        order.setMoney(order.getMoney().multiply(new BigDecimal(-1)));
        order.setTaxMoney(order.getMoney());
        order.setTotalCount(order.getTotalCount()*-1);
        order.setCreateTime(DateUtils.getNowDate());
        if (hospitalProductPurchaseOrderMapper.insertHospitalPurchaseOrder(order)>0){
            for (HospitalProductPurchaseOrderItem item : param.getItemList()){
                //新增item
                if (item.getItemId()!=null){
                    HospitalProductPurchaseOrderItem  purchaseOrderItem = itemMapper.selectHospitalPurchaseOrderItemByItemId(item.getItemId());
                    purchaseOrderItem.setLimitCount(purchaseOrderItem.getLimitCount()-item.getCount());
                    itemMapper.updateHospitalPurchaseOrderItem(purchaseOrderItem);
                }
                item.setHospitalId(order.getHospitalId());
                item.setOrderId(order.getOrderId());
                item.setOrderType(2);
                item.setItemId(null);
                if (itemMapper.insertHospitalPurchaseOrderItem(item)>0){
                    //修改商品信息
                    HospitalProduct product = hospitalProductMapper.selectHospitalProductByProductId(item.getProductId());
                    product.setTotalCount(product.getTotalCount()-item.getCount());
                    product.setSellCount(product.getSellCount()-item.getCount());
                    BigDecimal addition = item.getPrice().multiply(new BigDecimal(item.getCount()));
                    product.setCostPrice(product.getCostPrice().subtract(addition));
                    product.setUpdateTime(DateUtils.getNowDate());
                    hospitalProductMapper.updateHospitalProduct(product);
                    //记录日志
                    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("退货出库");
                    productLogsMapper.insertHospitalProductLogs(logs);
                }
            }

            return R.ok();
        }
        return R.error("退货失败");
    }
}
