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

import cn.hutool.core.util.NumberUtil;
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.basic.entity.BasicStorehouse;
import com.unis.crk.basic.entity.BasicWarehouse;
import com.unis.crk.basic.entity.vo.BasicWarehouseVo;
import com.unis.crk.basic.mapper.BasicWarehouseMapper;
import com.unis.crk.basic.service.IBasicWarehouseService;
import com.unis.crk.utils.ResultUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 库中仓房下的货位信息 服务实现类
 * </p>
 *
 * @author TangSong
 * @since 2021-11-25
 */
@Service
public class BasicWarehouseServiceImpl extends ServiceImpl<BasicWarehouseMapper, BasicWarehouse> implements IBasicWarehouseService {

    @Resource
    private BasicWarehouseMapper basicWarehouseMapper;


    /**
     * 按照条件查询库中仓房下的货位信息信息
     * @param pageQuery
     * @throws Exception
     */
    @Override
    public IPage<Map<String,Object>> findPageVo(BasicWarehouseVo pageQuery){
        //条形查询器
        QueryWrapper<BasicWarehouse> queryWrapper = new QueryWrapper<>();
            if (StringUtils.isNotBlank(pageQuery.getStorehouseId())){
                queryWrapper.eq("a.storehouse_id",pageQuery.getStorehouseId());
            }
            if (StringUtils.isNotBlank(pageQuery.getWarehouseName())){
            queryWrapper.like("a.warehouse_name", ResultUtil.getQueryWrapperformat(pageQuery.getWarehouseName()));
        }
            if (StringUtils.isNotBlank(pageQuery.getUnitId())){
            queryWrapper.eq("a.org_id",pageQuery.getUnitId());
        }
        queryWrapper.eq("a.del_flag",1); //只查询正常状态数据
        queryWrapper.orderByDesc("createDate");
        //分页相关
        Page<BasicWarehouse> p =new Page<>(pageQuery.getPage(),pageQuery.getLimit());

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

    @Override
    public List<Map<String, Object>> getWarehouseList(String unitId) {
        return basicWarehouseMapper.getWarehouseList(unitId);
    }

    @Override
    public List<Map<String, Object>> getWarehouseListByCclx(String unitId, String cclx) {
         return basicWarehouseMapper.getWarehouseListByCclx(unitId,cclx);
    }

    @Override
    public BasicWarehouse getOneHw(String id) {
        if (StrUtil.isNotBlank(id)){
            BasicWarehouse basicWarehouse = super.getById(id);
            return  basicWarehouse;
        }
        return null;
    }

    @Override
    public JSONObject removeHw(String id) {
        JSONObject jsonObject = new JSONObject();
        BasicWarehouse param = super.getById(id);
        if (param == null){
            jsonObject.set("msg","此货位不存在");
            return  jsonObject;
        }
        param.setDelFlag(2); //删除为2
        boolean b = super.updateById(param);
        if (!b) {
            jsonObject.set("msg","弃用仓房信息失败");
            return  jsonObject;
        }
        jsonObject.set("success",true);
        jsonObject.set("msg","弃用成功");
        return jsonObject;
    }

    @Override
    public JSONObject updateHw(BasicWarehouse param) {
        JSONObject jsonObject = new JSONObject();

        param.setUpdateDate(null);
        boolean b = super.updateById(param);
        if (!b) {
            jsonObject.set("msg","修改货位信息失败");
            return  jsonObject;
        }
        jsonObject.set("success",true);
        jsonObject.set("msg","修改成功");
        return jsonObject;
    }

    @Override
    public JSONObject saveHw(BasicWarehouse param) {
        JSONObject jsonObject = new JSONObject();
        param.setDelFlag(1);// 默认1正常
        param.setCreateDate(new Date());
        String orgId = param.getOrgId();
        param.setCreateName(orgId);
        param.setDepotId(orgId);

        boolean b = super.saveOrUpdate(param);
        if (!b) {
            jsonObject.set("msg","保存货位信息失败");
            return  jsonObject;
        }
        jsonObject.set("success",true);
        jsonObject.set("msg","保存成功");
        return jsonObject;
    }

}
