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

import cn.hutool.core.util.StrUtil;
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.OllectionReview;
import com.unis.crk.cplbusiness.entity.vo.OllectionReviewVo;
import com.unis.crk.cplbusiness.mapper.OllectionReviewMapper;
import com.unis.crk.cplbusiness.service.IOllectionReviewService;
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 OllectionReviewServiceImpl extends ServiceImpl<OllectionReviewMapper, OllectionReview> implements IOllectionReviewService {

    @Resource
    private OllectionReviewMapper ollectionReviewMapper;

    @Autowired
    private IDeleteDataRecordService deleteDataRecordService;

    /**
     *修改领用审核
     * @param param 领用审核业务数据
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JSONObject update(OllectionReview 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);
        OllectionReview ollectionReview = ollectionReviewMapper.selectById(id);
        if(!super.removeById(id)){
            jsonObject.set("msg", "删除数据失败");
            return jsonObject;
        }
        deleteDataRecordService.save(id,"ollection_review",ollectionReview.getUnitid(),new Date());
        jsonObject.set("success", true);
        jsonObject.set("msg", "删除成功");
        return jsonObject;
        }

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

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


}
