package com.prolog.eis.service.out.impl;

import com.prolog.eis.dao.out.ContainerStoreChangeMapper;
import com.prolog.eis.model.container.ContainerInfoDetail;
import com.prolog.eis.model.container.ContainerStoreChange;
import com.prolog.eis.service.out.ContainerStoreChangeService;
import com.prolog.eis.vo.GoodsInfoVO;
import com.prolog.framework.core.restriction.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @author zmr
 * @description: 容器库存变动表-服务实现类
 * @date 2023-08-30 12:24
 */
@Service
public class ContainerStoreChangeServiceImpl implements ContainerStoreChangeService {

    @Autowired
    private ContainerStoreChangeMapper containerStoreChangeMapper;

    /**
     * 新增数据
     *
     * @param containerStoreChange 容器库存变动表
     */
    @Override
    public long save(ContainerStoreChange containerStoreChange) {
        return containerStoreChangeMapper.save(containerStoreChange);
    }

    /**
     * 新增批量数据
     *
     * @param containerStoreChangeList 容器库存变动表
     */
    @Override
    public long saveBatch(List<ContainerStoreChange> containerStoreChangeList) {
        return containerStoreChangeMapper.saveBatch(containerStoreChangeList);
    }

    /**
     * 查询多条数据
     *
     * @param paramMap 查询起始位置
     * @return 对象列表
     */
    @Override
    public List<ContainerStoreChange> findByMap(Map<String, Object> paramMap) {
        return containerStoreChangeMapper.findByMap(paramMap, ContainerStoreChange.class);
    }

    /**
     * 查询单条数据
     *
     * @param paramMap 查询起始位置
     * @return 对象
     */
    @Override
    public ContainerStoreChange findOneByMap(Map<String, Object> paramMap) {
        List<ContainerStoreChange> dataList = findByMap(paramMap);
        if(dataList.isEmpty()) {
            return null;
        }
        return dataList.get(0);
    }

    /**
     * 查询多条数据
     *
     * @param criteria 查询起始位置
     * @return 对象列表
     */
    @Override
    public List<ContainerStoreChange> findByCriteria(Criteria criteria) {
        return containerStoreChangeMapper.findByCriteria(criteria);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ContainerStoreChange findById(int id) {
        return containerStoreChangeMapper.findById(id, ContainerStoreChange.class);
    }

    /**
     * 修改数据
     *
     * @param containerStoreChange 实例对象
     */
    @Override
    public long update(ContainerStoreChange containerStoreChange) {
        return containerStoreChangeMapper.update(containerStoreChange);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     */
    @Override
    public long deleteById(int id) {
        return containerStoreChangeMapper.deleteById(id, ContainerStoreChange.class);
    }

    /**
     * 通过参数删除数据
     *
     * @param paramMap 参数
     */
    @Override
    public long deleteByMap(Map<String, Object> paramMap) {
        return containerStoreChangeMapper.deleteByMap(paramMap, ContainerStoreChange.class);
    }

    /**
     * 通过参数查询总条数
     *
     * @param paramMap 参数
     */
    @Override
    public long findCountByMap(Map<String, Object> paramMap) {
        return containerStoreChangeMapper.findCountByMap(paramMap, ContainerStoreChange.class);
    }

    @Override
    public void saveContainerStoreChange(String containerNo, String containerSubNo, GoodsInfoVO vo, int taskType,
                                         BigDecimal targetQty, BigDecimal changeQty, String msg, String upperSystemId) {
        final ContainerStoreChange storeChange = new ContainerStoreChange();
        storeChange.setContainerNo(containerNo);
        storeChange.setContainerSubNo(containerSubNo);
        //storeChange.setBizPropertyId(bizPropertyId);
        storeChange.setItemId(vo.getGoodsId());
        storeChange.setLotId(vo.getLotId());
        storeChange.setOwnerId(vo.getOwnerId());
        storeChange.setPackId(vo.getPackId());
        storeChange.setTaskType(taskType);
        storeChange.setSoureQty(targetQty.subtract(changeQty));
        storeChange.setChangeQty(changeQty);
        storeChange.setTargetQty(targetQty);
        storeChange.setUpperSystemId(upperSystemId);
        storeChange.setRemark(msg);
        storeChange.setCreateTime(LocalDateTime.now());
        containerStoreChangeMapper.save(storeChange);
    }

    @Override
    public void saveStoreChange(ContainerInfoDetail containerInfoDetail, BigDecimal change, BigDecimal target, Integer workType, String remark) {
        ContainerStoreChange changeChange = new ContainerStoreChange();
        changeChange.setContainerNo(containerInfoDetail.getContainerNo());
        changeChange.setContainerSubNo(containerInfoDetail.getContainerSubNo());
        changeChange.setTaskType(workType);
        changeChange.setSoureQty(containerInfoDetail.getQty());
        changeChange.setChangeQty(change);
        changeChange.setTargetQty(target);
        changeChange.setRemark(remark);
        changeChange.setCreateTime(LocalDateTime.now());
        changeChange.setUpdateTime(LocalDateTime.now());
        changeChange.setPackId(containerInfoDetail.getPackId());
        changeChange.setOwnerId(containerInfoDetail.getOwnerId());
        changeChange.setLotId(containerInfoDetail.getLotId());
        changeChange.setItemId(containerInfoDetail.getItemId());
        containerStoreChangeMapper.save(changeChange);
    }
}
