package com.yh.saas.plugin.yiliangyiyun.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.winsea.svc.base.security.util.AuthSecurityUtils;
import com.yh.saas.common.support.util.IdGenerator;
import com.yh.saas.common.support.util.StringUtils;
import com.yh.saas.plugin.yiliangyiyun.constant.NumberConstant;
import com.yh.saas.plugin.yiliangyiyun.entity.*;
import com.yh.saas.plugin.yiliangyiyun.mapper.CostManagementInfoMapper;
import com.yh.saas.plugin.yiliangyiyun.service.IContractGoodsInfoService;
import com.yh.saas.plugin.yiliangyiyun.service.ICostManagementInfoService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yh.saas.plugin.yiliangyiyun.service.IWeightedDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * <p>
 * 成本管理信息 服务实现类
 * </p>
 *
 * @author Gdc
 * @since 2021-10-09
 */
@Service
public class CostManagementInfoServiceImpl extends ServiceImpl<CostManagementInfoMapper, CostManagementInfo> implements ICostManagementInfoService {
    @Autowired
    private IWeightedDetailsService weightedDetailsService;


    /**
     * 列表
     *
     * @param costManagementInfo
     * @return
     */
    @Override
    public List<CostManagementInfo> selectCostManagementInfo(CostManagementInfo costManagementInfo) {
        String compId = costManagementInfo.getCompId();
        if(compId == null){
            compId = AuthSecurityUtils.getCurrentUserInfo().getCompId();
        }
        Wrapper<CostManagementInfo> costManagementInfoWrapper = new EntityWrapper<>();
        costManagementInfoWrapper.eq("warehouse_type", costManagementInfo.getWarehouseType())
                                 .eq("comp_id", compId);
        if (costManagementInfo.getWarehouseId()!=null&&!costManagementInfo.getWarehouseId().isEmpty()){
            costManagementInfoWrapper.eq("warehouse_id", costManagementInfo.getWarehouseId());
        }
        if (costManagementInfo.getWarehouseName()!=null&&!costManagementInfo.getWarehouseName().isEmpty()){
            costManagementInfoWrapper.like("warehouse_name", "%" + costManagementInfo.getWarehouseName() + "%");
        }

        costManagementInfoWrapper.gt("storage", 0);
        costManagementInfoWrapper.orderBy("warehouse_name",false);

        List<CostManagementInfo> costManagementInfoList = this.selectList(costManagementInfoWrapper);
        return costManagementInfoList;
    }


    /**
     * 编辑信息
     *
     * @param costManagementInfo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String editCostManagementInfo(CostManagementInfo costManagementInfo) {
        boolean one = this.updateById(costManagementInfo);
        CostManagementInfo costManagementInfo1=this.selectById(costManagementInfo.getId());
        WeightedDetails weightedDetails=new WeightedDetails();
        //新增主键id
        weightedDetails.setId(IdGenerator.generateUUID());
        weightedDetails.setCompId(costManagementInfo1.getCompId());
        weightedDetails.setWarehouseName(costManagementInfo1.getWarehouseName());
        weightedDetails.setWarehouseId(costManagementInfo1.getWarehouseId());
        weightedDetails.setCostAfter(costManagementInfo1.getCost());
        weightedDetails.setCostBefore(Float.valueOf(String.valueOf(costManagementInfo1.getCost())));
        weightedDetails.setGoodsName(costManagementInfo1.getGoodsName());
        weightedDetails.setGoodsNameKey(costManagementInfo1.getGoodsNameKey());
        weightedDetailsService.insert(weightedDetails);
        if (one) {
            return "OK";
        } else {
            return "NG";
        }
    }

}
