package com.fjhx.service.flow;

import com.fjhx.entity.supplier.SupplierPrice;
import com.fjhx.entity.supplier.SupplierPriceChange;
import com.fjhx.params.JumpVo;
import com.fjhx.params.supplier.SupplierPriceVo;
import com.fjhx.service.supplier.SupplierPriceChangeService;
import com.fjhx.service.supplier.SupplierPriceService;
import com.fjhx.utils.ExampleAbstract;
import com.fjhx.utils.FlowConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 供应商价格维护修改审批
 */
@Service
public class SupplierPriceUpdateFlow {

    @Autowired
    private SupplierPriceService supplierPriceService;

    @Autowired
    private SupplierPriceChangeService supplierPriceChangeService;

    private final FlowConstructor flowConstructor = FlowConstructor.init(new ExampleAbstract() {

        @Override
        public String getCode() {
            return "js_supplierPriceUpdate";
        }

        @Override
        public void end() {
            SupplierPriceVo supplierPriceVo = getCacheData(SupplierPriceVo.class);

            // 添加价格变更记录
            SupplierPrice supplierPrice = supplierPriceService.getById(supplierPriceVo.getId());
            SupplierPriceChange supplierPriceChange = new SupplierPriceChange();
            supplierPriceChange.setSupplierPriceId(supplierPrice.getId());
            supplierPriceChange.setSupplierId(supplierPrice.getSupplierId());
            supplierPriceChange.setMaterialId(supplierPrice.getMaterialId());
            supplierPriceChange.setOldPrice(supplierPrice.getPrice());
            supplierPriceChange.setNewPrice(supplierPriceVo.getPrice());
            supplierPriceChange.setRemark(supplierPriceVo.getRemarks());
            supplierPriceChangeService.save(supplierPriceChange);

            // 变更价格
            supplierPriceService.updateById(supplierPriceVo);
        }

    });

    @Transactional(rollbackFor = Exception.class)
    public void create(SupplierPriceVo supplierPriceVo) {
        flowConstructor.create(supplierPriceVo.getId(), "价格维护", supplierPriceVo.getRemarks(), supplierPriceVo);
    }

    @Transactional(rollbackFor = Exception.class)
    public void jump(JumpVo jumpVo) {
        flowConstructor.jump(jumpVo);
    }

}
