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.conditions.update.UpdateWrapper;
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.BasicStorehouseVo;
import com.unis.crk.basic.mapper.BasicStorehouseMapper;
import com.unis.crk.basic.service.IBasicStorehouseService;
import com.unis.crk.basic.service.IBasicWarehouseService;
import com.unis.crk.business.qcrk.entity.DataCrkywJyxxFlt001;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
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 BasicStorehouseServiceImpl extends ServiceImpl<BasicStorehouseMapper, BasicStorehouse> implements IBasicStorehouseService {

    @Resource
    private BasicStorehouseMapper basicStorehouseMapper;

    @Autowired
    private IBasicWarehouseService basicWarehouseService;
    /**
     * 按照条件查询库点仓房信息信息
     * @param pageQuery
     * @throws Exception
     */
    @Override
    public IPage<Map<String,Object>> findPageVo(BasicStorehouseVo pageQuery){
        {
        //条形查询器
        QueryWrapper<BasicStorehouse> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(pageQuery.getStorehouseId())){
            //queryWrapper.eq("a.storehouse_id",pageQuery.getStorehouseId());
            /*wys:20230522,处理为模糊查询*/
            queryWrapper.and( warpper -> warpper.like("a.storehouse_id",pageQuery.getStorehouseId())
                    .or().like("a.storehouse_name",pageQuery.getStorehouseId()));
        }
        if (StringUtils.isNotBlank(pageQuery.getUnitId())){
            queryWrapper.eq("a.org_id",pageQuery.getUnitId());
        }
            queryWrapper.eq("a.del_flag",1); //默认正常使用
            //分页相关
        Page<BasicStorehouse> p =new Page<>(pageQuery.getPage(),pageQuery.getLimit());

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

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

    @Override
    public JSONObject saveChhw(BasicStorehouse param) throws Exception {
        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;
        }
        List<BasicWarehouse> basicWarehouseList = param.getBasicWarehouseList(); //得到相关货位信息
        if (!basicWarehouseList.isEmpty()){
            String storehouseId = param.getStorehouseId(); //仓房id
            BigDecimal warehouseCapacity = new BigDecimal(0.0);
            for (int i = 0; i < basicWarehouseList.size(); i++) {
                BasicWarehouse basicWarehouse = basicWarehouseList.get(i);
                basicWarehouse.setDepotId(orgId);
                basicWarehouse.setDelFlag(1); //正常
                basicWarehouse.setOrgId(orgId);
                basicWarehouse.setCreateDate(new Date());
                basicWarehouse.setCreateName(param.getOrgId());
                basicWarehouse.setStorehouseId(storehouseId); //仓房id
                warehouseCapacity = NumberUtil.add(warehouseCapacity,basicWarehouse.getWarehouseCapacity()); //相加
            }
            if(NumberUtil.compare(param.getActualCapacity().doubleValue(),warehouseCapacity.doubleValue()) < 0) //x<y 返回小于0
            {
                throw new  Exception("货位容量超过仓房实际容量");
            }
            if (!basicWarehouseService.saveOrUpdateBatch(basicWarehouseList)) { //保存货位信息
                throw new  Exception("保存货位信息失败");
            }
        }
        jsonObject.set("success",true);
        jsonObject.set("msg","保存成功");
        return jsonObject;
    }

    @Override
    public JSONObject updateChhw(BasicStorehouse param)throws Exception {
            JSONObject jsonObject = new JSONObject();

            param.setUpdateDate(null);
            boolean b = super.updateById(param);
            if (!b) {
                jsonObject.set("msg","修改仓房信息失败");
                return  jsonObject;
            }
            List<BasicWarehouse> basicWarehouseList = param.getBasicWarehouseList(); //得到相关货位信息
            if (!basicWarehouseList.isEmpty()){
                String storehouseId = param.getStorehouseId(); //仓房id
                String orgId = param.getOrgId();
                BigDecimal warehouseCapacity = new BigDecimal(0.0);
                for (int i = 0; i < basicWarehouseList.size(); i++) {
                    BasicWarehouse basicWarehouse = basicWarehouseList.get(i);
                    basicWarehouse.setDepotId(orgId);
                    basicWarehouse.setDelFlag(1); //正常
                    basicWarehouse.setOrgId(orgId);
                    basicWarehouse.setStorehouseId(storehouseId); //仓房id
                    if (StrUtil.isNotBlank(basicWarehouse.getWarehouseId())){
                        basicWarehouse.setUpdateDate(new Date());
                        basicWarehouse.setUpdateName(param.getOrgId());
                    }else {
                        basicWarehouse.setCreateDate(new Date());
                        basicWarehouse.setCreateName(param.getOrgId());
                    }
                    warehouseCapacity = NumberUtil.add(warehouseCapacity,basicWarehouse.getWarehouseCapacity()); //相加
                }
                if(NumberUtil.compare(param.getActualCapacity().doubleValue(),warehouseCapacity.doubleValue()) < 0) //x<y 返回小于0
                {
                    throw new  Exception("货位容量超过仓房实际容量");
                }
                if (!basicWarehouseService.saveOrUpdateBatch(basicWarehouseList)) { //修改货位信息
                    throw new  Exception("修改货位信息失败");
                }
            }
            jsonObject.set("success",true);
            jsonObject.set("msg","修改成功");
            return jsonObject;
    }

    @Override
    public JSONObject deleteChhw(String id)throws Exception {
        {
            JSONObject jsonObject = new JSONObject();
            BasicStorehouse 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;
            }
            UpdateWrapper<BasicWarehouse> basicWarehouseUpdateWrapper = new UpdateWrapper<>();
            basicWarehouseUpdateWrapper.eq("storehouse_id",id);
            basicWarehouseUpdateWrapper.set("del_flag",2); //删除状态

            if (! basicWarehouseService.update(basicWarehouseUpdateWrapper)) {
                throw new Exception("弃用仓房对应货位失败");
            }
            jsonObject.set("success",true);
            jsonObject.set("msg","弃用成功");
            return jsonObject;
        }
    }

    @Override
    public BasicStorehouse getOneChHw(String id) {
        if (StrUtil.isNotBlank(id)){
            BasicStorehouse basicStorehouse = super.getById(id);
            QueryWrapper<BasicWarehouse> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("*").lambda();
            queryWrapper.eq("storehouse_id",id);
            queryWrapper.eq("del_flag",1); //正常使用
            queryWrapper.orderByAsc("warehouse_code"); //以货位编码排序
            basicStorehouse.setBasicWarehouseList(basicWarehouseService.list(queryWrapper));
            return  basicStorehouse;
        }
        return null;
    }

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

}
