package com.unis.crk.basic.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.basic.entity.BasicWarehouse;
import com.unis.crk.basic.entity.StorageGrainCard;
import com.unis.crk.basic.entity.vo.StorageGrainCardVo;
import com.unis.crk.basic.mapper.StorageGrainCardMapper;
import com.unis.crk.basic.service.IBasicWarehouseService;
import com.unis.crk.basic.service.IStorageGrainCardService;
import com.unis.crk.enums.CrkStatus;
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.Map;

/**
 * <p>
 * 粮情卡 服务实现类
 * </p>
 *
 * @author TangSong
 * @since 2022-02-10
 */
@Service
public class StorageGrainCardServiceImpl extends ServiceImpl<StorageGrainCardMapper, StorageGrainCard> implements IStorageGrainCardService {

    @Resource
    private StorageGrainCardMapper storageGrainCardMapper;

    @Autowired
    private IBasicWarehouseService basicWarehouseService;
    /**
     *保存粮情卡
     * @param grainCard 粮情卡业务数据
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JSONObject saveCard(StorageGrainCard grainCard) throws Exception {
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("success",false);
        if (!super.saveOrUpdate(grainCard)){
            jsonObject.set("msg","保存粮情卡失败");
            return jsonObject;
        }
        String hwId = grainCard.getWarehouseId(); //货位id
        BasicWarehouse basicWarehouse = basicWarehouseService.getById(hwId);
        if (basicWarehouse == null){
            throw  new Exception("此货位不存在");
        }
        basicWarehouse.setCrkStatus(CrkStatus.Fc.value().toString()); //1表示封仓
        if (!basicWarehouseService.updateById(basicWarehouse)){
            throw  new Exception("修改仓房状态失败");
        }
        jsonObject.set("success",true);
        jsonObject.set("msg","保存成功");
        return jsonObject;
    }
    /**
     *修改粮情卡
     * @param grainCard 粮情卡业务数据
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JSONObject update(StorageGrainCard grainCard)throws Exception{
            JSONObject jsonObject = new JSONObject();
             jsonObject.set("success",false);
            if (!super.updateById(grainCard)){
                jsonObject.set("msg","修改粮情卡失败");
                return jsonObject;
            }
            String hwId = grainCard.getWarehouseId(); //货位id
            BasicWarehouse basicWarehouse = basicWarehouseService.getById(hwId);
            if (basicWarehouse == null){
                throw  new Exception("此货位不存在");
            }
            basicWarehouse.setCrkStatus(CrkStatus.Fc.value().toString()); //1表示封仓
            if (!basicWarehouseService.updateById(basicWarehouse)){
                throw  new Exception("修改仓房状态失败");
            }
            jsonObject.set("success",true);
            jsonObject.set("msg","修改成功");
            return jsonObject;
    }
    /**
     *删除粮情卡数据
     * @param id 粮情卡主键尖
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JSONObject delete(String id)throws Exception{
            JSONObject jsonObject = new JSONObject();
            jsonObject.set("success",false);
            StorageGrainCard grainCard = super.getById(id);
            if (grainCard == null){
                jsonObject.set("msg","此条数据已经不存在");
                return jsonObject;
            }
            if (!super.removeById(id)){
                jsonObject.set("msg","删除粮情卡失败");
                return jsonObject;
            }
        String hwId = grainCard.getWarehouseId(); //货位id
        BasicWarehouse basicWarehouse = basicWarehouseService.getById(hwId);
            if (basicWarehouse == null){
                throw  new Exception("此货位不存在");
            }
            basicWarehouse.setCrkStatus(CrkStatus.None.value().toString()); //0表示无状态
            if (!basicWarehouseService.updateById(basicWarehouse)){
                throw  new Exception("修改仓房状态失败");
            }
            jsonObject.set("success",true);
            jsonObject.set("msg","删除成功");
            return jsonObject;
        }

    /**
     * 按照条件查询粮情卡信息
     * @param pageQuery
     * @throws Exception
     */
    @Override
    public IPage<Map<String,Object>> findPageVo(StorageGrainCardVo pageQuery){
        //条形查询器
        QueryWrapper<StorageGrainCard> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(pageQuery.getUnitId())){
             queryWrapper.eq("a.org_id",pageQuery.getUnitId());
        }
        if (StringUtils.isNotBlank(pageQuery.getStorehouseName())){
             queryWrapper.like("a.storehouse_name", ResultUtil.getQueryWrapperformat(pageQuery.getStorehouseName()));
        }
        queryWrapper.orderByDesc("a.accept_date"); //驗收日期倒序
        //分页相关
        Page<StorageGrainCard> p =new Page<>(pageQuery.getPage(),pageQuery.getLimit());

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

    @Override
    public Map<String, Object> getOne(String id) {

        return storageGrainCardMapper.getOne(id);
    }

}
