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

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.winsea.svc.base.base.util.DateUtils;
import com.yh.saas.common.support.util.IdGenerator;
import com.yh.saas.plugin.yiliangyiyun.entity.*;
import com.yh.saas.plugin.yiliangyiyun.mapper.InventoryCostInfoMapper;
import com.yh.saas.plugin.yiliangyiyun.service.IInventoryCostInfoService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.httpclient.util.DateUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * <p>
 * 库点费用信息 服务实现类
 * </p>
 *
 * @author Gdc
 * @since 2021-10-08
 */
@Service
public class InventoryCostInfoServiceImpl extends ServiceImpl<InventoryCostInfoMapper, InventoryCostInfo> implements IInventoryCostInfoService {

    /**
     * 列表
     *
     * @param inventoryCostInfo
     * @return
     */
    @Override
    public Page<InventoryCostInfo> selectInventoryCostInfo(InventoryCostInfo inventoryCostInfo) throws ParseException {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy");
        Date start=simpleDateFormat.parse(inventoryCostInfo.getYear());
        Date end=simpleDateFormat.parse(inventoryCostInfo.getYear()+1);
        Page<InventoryCostInfo> inventoryCostInfoPage = this.selectPage(inventoryCostInfo.getQueryPage(), new EntityWrapper<InventoryCostInfo>()
                .eq("warehouse_id", inventoryCostInfo.getWarehouseId())
                .gt("cost_date",start)
                .lt("cost_date",end)
                .orderBy("cost_date",false));
        int count = this.selectCount( new EntityWrapper<InventoryCostInfo>()
                .eq("warehouse_id", inventoryCostInfo.getWarehouseId())
                .gt("cost_date",start)
                .lt("cost_date",end)
                .orderBy("cost_date",false));
        inventoryCostInfoPage.setSize(count);
        return inventoryCostInfoPage;
    }

    /**
     * 添加信息
     *
     * @param inventoryCostInfo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addInventoryCostInfo(InventoryCostInfo inventoryCostInfo) {
        Date now=new Date();
        //新增主键id
        inventoryCostInfo.setId(IdGenerator.generateUUID());
        inventoryCostInfo.setCostDate(now);
        // 操作主表数据
        boolean one = this.insert(inventoryCostInfo);
        if (one) {
            return "OK";
        } else {
            return "NG";
        }
    }


    /**
     * 编辑信息
     *
     * @param inventoryCostInfo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String editInventoryCostInfo(InventoryCostInfo inventoryCostInfo) {
        boolean one = this.updateById(inventoryCostInfo);
        if (one) {
            return "OK";
        } else {
            return "NG";
        }
    }

}
