package com.unis.crk.business.qcrk.service.impl;

import com.unis.crk.aop.SystemLog;
import com.unis.crk.business.qcrk.entity.DataCrkywZcDefault;
import com.unis.crk.business.qcrk.mapper.DataCrkywZcDefaultMapper;
import com.unis.crk.business.qcrk.service.IDataCrkywZcDefaultService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unis.crk.common.service.IDeleteDataRecordService;
import com.unis.crk.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang.StringUtils;

import java.util.Date;
import java.util.Map;
import cn.hutool.json.JSONObject;
import com.unis.crk.business.qcrk.entity.vo.DataCrkywZcDefaultVo;
import com.unis.crk.business.qcrk.entity.vo.Qcrk;

import javax.annotation.Resource;

/**
 * <p>
 * 汽车出入库-值仓 服务实现类
 * </p>
 *
 * @author TangSong
 * @since 2022-06-02
 */
@Service
public class DataCrkywZcDefaultServiceImpl extends ServiceImpl<DataCrkywZcDefaultMapper, DataCrkywZcDefault> implements IDataCrkywZcDefaultService {

    @Resource
    private DataCrkywZcDefaultMapper dataCrkywZcDefaultMapper;
    @Autowired
    private IDeleteDataRecordService deleteDataRecordService;

    /**
     * 按照条件查询汽车出入库-值仓信息
     * @param pageQuery
     * @throws Exception
     */
    @Override
    public IPage<Map<String,Object>> findPageVo(DataCrkywZcDefaultVo pageQuery){
        //条形查询器
        QueryWrapper<DataCrkywZcDefault> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(pageQuery.getUnitId())){
            queryWrapper.eq("unitId",pageQuery.getUnitId());
        }
        if (StringUtils.isNotBlank(pageQuery.getBizNo())){
            queryWrapper.like("bizNo", ResultUtil.getQueryWrapperformat(pageQuery.getBizNo()));
        }
        if (StringUtils.isNotBlank(pageQuery.getCcsslx())){
            queryWrapper.eq("ccsslx",pageQuery.getCcsslx());
        }
        if (StringUtils.isNotBlank(pageQuery.getZcsjStart()) && StringUtils.isNotBlank(pageQuery.getZcsjEnd())){
            queryWrapper.apply(" DATE_FORMAT(ifnull(zcsj,NOW()),'%Y-%m-%d') >=  '"+pageQuery.getZcsjStart()+"' AND DATE_FORMAT(ifnull(zcsj,NOW()),'%Y-%m-%d') <= '"+pageQuery.getZcsjEnd()+" 23:59:59'");
        }
        queryWrapper.orderByDesc("zcsj");
        //分页相关
        Page<DataCrkywZcDefault> p =new Page<>(pageQuery.getPage(),pageQuery.getLimit());

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

    /**
     * 保存值仓信息
     *
     * @param zcDefault
     * @return
     */
    @Override
    @SystemLog(module = "原粮入库新增值仓", methods = "新增",uploadName = "yuliang.rk")
    public JSONObject saveZc(DataCrkywZcDefault zcDefault) {
        JSONObject  jsonObject = new JSONObject();
        zcDefault.setBuildDate(new Date());
        zcDefault.setPeriodID("12");
        zcDefault.setPreorder(0);
        zcDefault.setShoworder(0);
        zcDefault.setBuilder(zcDefault.getUnitID());
        boolean b = super.save(zcDefault);
        jsonObject.set("success",b);
        jsonObject.set("msg",b?"保存成功":"保存失敗");
        return jsonObject;
    }

    /**
     * 修改值仓信息
     *
     * @param zcDefault
     * @return
     */
    @Override
    @SystemLog(module = "原粮入库修改值仓", methods = "修改",uploadName = "yuliang.rk")
    public JSONObject updateZc(DataCrkywZcDefault zcDefault) {
        JSONObject  jsonObject = new JSONObject();
        zcDefault.setModifyDate(new Date());
        boolean b = super.updateById(zcDefault);
        jsonObject.set("success",b);
        jsonObject.set("msg",b?"修改成功":"修改失敗");
        return jsonObject;
    }

    /**
     * 删除值仓信息
     *
     * @param id
     * @return
     */
    @Override
    @SystemLog(module = "原粮入库删除值仓", methods = "删除",uploadName = "yuliang.rk")
    public JSONObject deleteZc(String id) {
        JSONObject  jsonObject = new JSONObject();
        DataCrkywZcDefault dataCrkywZcDefault = dataCrkywZcDefaultMapper.selectById(id);
        boolean b = super.removeById(id);
        jsonObject.set("success",b);
        if (!b) {
            jsonObject.set("msg","删除失败");
            return  jsonObject;
        }
        //保存删除数据记录
        deleteDataRecordService.save(id,"data_crkyw_zc_default",dataCrkywZcDefault.getUnitID(),new Date());
        jsonObject.set("msg","删除成功");
        return jsonObject;
    }

}
