package com.unis.crk.cplbusiness.service.impl;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unis.crk.common.service.IDeleteDataRecordService;
import com.unis.crk.cplbusiness.entity.NventoryAdjustment;
import com.unis.crk.cplbusiness.entity.vo.NventoryAdjustmentVo;
import com.unis.crk.cplbusiness.mapper.NventoryAdjustmentMapper;
import com.unis.crk.cplbusiness.service.INventoryAdjustmentService;
import com.unis.crk.utils.ResultUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 库存调整 服务实现类
 * </p>
 *
 * @author TangSong
 * @since 2022-05-23
 */
@Service
public class NventoryAdjustmentServiceImpl extends ServiceImpl<NventoryAdjustmentMapper, NventoryAdjustment> implements INventoryAdjustmentService {

    @Resource
    private NventoryAdjustmentMapper nventoryAdjustmentMapper;
    @Autowired
    private IDeleteDataRecordService deleteDataRecordService;


    /**
     *修改库存调整
     * @param param 库存调整业务数据
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JSONObject update(NventoryAdjustment param){
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("success",false);
        if(!super.saveOrUpdate(param)){
            jsonObject.set("msg", "修改数据失败");
            return jsonObject;
        }
        jsonObject.set("success", true);
        jsonObject.set("msg", "修改成功");
        return jsonObject;
    }
    /**
     *删除库存调整数据
     * @param id 库存调整主键尖
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JSONObject delete(String id){
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("success",false);
        NventoryAdjustment nventoryAdjustment = nventoryAdjustmentMapper.selectById(id);
        if(!super.removeById(id)){
            jsonObject.set("msg", "删除数据失败");
            return jsonObject;
        }
        deleteDataRecordService.save(id,"nventory_adjustment",nventoryAdjustment.getUnitid(),new Date());
        jsonObject.set("success", true);
        jsonObject.set("msg", "删除成功");
        return jsonObject;
        }

    /**
     * 按照条件查询库存调整信息
     * @param pageQuery
     * @throws Exception
     */
    @Override
    public IPage<Map<String,Object>> findPageVo(NventoryAdjustmentVo pageQuery){
        {
        //条形查询器
        QueryWrapper<NventoryAdjustment> queryWrapper = new QueryWrapper<>();
        //分页相关
        Page<NventoryAdjustment> p =new Page<>(pageQuery.getPage(),pageQuery.getLimit());
            if(StringUtils.isNotBlank(pageQuery.getUnitId())) {
                queryWrapper.eq("a.unitid", pageQuery.getUnitId());
            }
            if(StringUtils.isNotBlank(pageQuery.getCommodityName())){
                queryWrapper.like("mxpz_enum.sjjlspmc", ResultUtil.getQueryWrapperformat(pageQuery.getCommodityName()));
            }
            if (StringUtils.isNotBlank(pageQuery.getYwStart()) && StringUtils.isNotBlank(pageQuery.getYwEnd())){
                queryWrapper.apply(" DATE_FORMAT(ifnull(adjustment_time,NOW()),'%Y-%m-%d') >=  '"+pageQuery.getYwStart()+"' AND DATE_FORMAT(ifnull(adjustment_time,NOW()),'%Y-%m-%d') <= '"+pageQuery.getYwEnd()+" 23:59:59'");

            }
        IPage<Map<String,Object>> page = nventoryAdjustmentMapper.selectMyMapsPage(p,queryWrapper);
        return page;
        }
    }


}
