package com.chinaitop.depot.intelligent.basicdata.service.impl;

import com.chinaitop.depot.intelligent.basicdata.mapper.WareHouseBasicInfoMapper;
import com.chinaitop.depot.intelligent.basicdata.service.WareHouseBasicInfoService;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author qingsong.han
 * @description:
 * @create 2018-12-17 14:13
 */
@SuppressWarnings("all")
@Service
public class WareHouseBasicInfoServiceImpl implements WareHouseBasicInfoService {

    @Resource
    private WareHouseBasicInfoMapper wareHouseBasicInfoMapper;

    @Override
    public Map<String, Object> getStorehouseInfo(Integer unitId, String libraryType) {
        Map<String, Object> map = new HashMap<>();
        if (unitId != null) {
            map.put("org_id", unitId);
        }
        if (StringUtils.isNotBlank(libraryType)) {
            map.put("library_type", libraryType);
        }
        List<Map<String, Object>> listMap = wareHouseBasicInfoMapper.getStorehouseInfo(map);
        Map<String, Object> objMap = new HashMap<>();
        for (Map<String, Object> stringObjectMap : listMap) {
            String keys = "";
            for (String key : stringObjectMap.keySet()) {
                if (key.equals("storehouse_code")) {
                    keys = stringObjectMap.get(key).toString();
                }
            }
            objMap.put(keys, stringObjectMap);
        }
        Map<String, Object> reMap = new HashMap<>();
        reMap.put("storehouseCode", objMap);
        return reMap;
    }

    /**
     * 同步数据递增字段获取Max值
     *
     * @param tableName 表名
     * @param column1   列和条件参数1 (sitecode)
     * @param column2   参数2
     * @param column3   参数3
     * @return 列参数的Max + 1
     */
    @Override
    public Map<String, Object> getCode(String tableName, String column1, String column2, String column3) {
        // 开始值(数据库表在插入第一条数据时)
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotBlank(tableName))
            map.put("tableName", tableName);
        if (StringUtils.isNotBlank(column1))
            map.put("column1", column1);
        if (StringUtils.isNotBlank(column2))
            map.put("column2", column2);
        if (StringUtils.isNotBlank(column2))
            map.put("column3", column3);
        Map<String, Object> resultMax = wareHouseBasicInfoMapper.getColumn1Max(map);
        Map<String, Object> resultMap = new HashMap<>();
        if (resultMax != null) {
            for (String s : resultMax.keySet()) {
                resultMap.put(s, (Integer.valueOf(resultMax.get(s).toString())) + 1);
            }
        } else {
            resultMap.put("column1", "10000");
            resultMap.put("column2", "10000");
            resultMap.put("column3", "10000");
        }
        return resultMap;
    }

    /**
     * 同步数据递增字段获取Max值-可变参数
     * <p>
     * param1 tableName 表名
     * param2 column1   列和条件参数1 (sitecode)
     * param3 column2   参数2
     * param4 column3   参数3
     *
     * @return 列参数的Max + 1
     */
    @Override
    public Map<String, Object> getCode(String... str) {
        // 开始值(数据库表在插入第一条数据时)
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotBlank(str[0]))
            map.put("tableName", str[0]);
        if (StringUtils.isNotBlank(str[1]))
            map.put("column1", str[1]);
        if (str.length >= 3 && StringUtils.isNotBlank(str[2]))
            map.put("column2", str[2]);
        if (str.length >= 4 && StringUtils.isNotBlank(str[3]))
            map.put("column3", str[3]);
        Map<String, Object> resultMax = wareHouseBasicInfoMapper.getColumn1Max(map);
        Map<String, Object> resultMap = new HashMap<>();
        if (resultMax != null) {
            for (String s : resultMax.keySet()) {
                resultMap.put(s, (Integer.valueOf(resultMax.get(s).toString())) + 1);
            }
        } else {
            resultMap.put("column1", "10000");
            resultMap.put("column2", "10000");
            resultMap.put("column3", "10000");
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> getStorehouse(Integer orgId, String libraryType, String cfCdoe) {
        Map<String, Object> map = new HashMap<>();
        if (orgId != null) {
            map.put("org_id", orgId);
        }
        if (StringUtils.isNotBlank(libraryType)) {
            map.put("library_type", libraryType);
        }else {
            map.put("library_type", "0");
        }
        if (StringUtils.isNotBlank(cfCdoe)) {
            map.put("cfCdoe", cfCdoe);
        }
        List<Map<String, Object>> listMap = wareHouseBasicInfoMapper.getStorehouseInfo(map);
        Map<String, Object> storehouseMap = null;
        if (listMap.size() > 0) {
            storehouseMap = listMap.get(0);
            //粮食等级
            if (storehouseMap.get("dj") != null) {
                String dj = wareHouseBasicInfoMapper.getEnum(storehouseMap.get("dj").toString());
                storehouseMap.put("dj", dj);
            }
            //粮食性质
            if (storehouseMap.get("hwxz") != null) {
                String hwxz = wareHouseBasicInfoMapper.getEnum(storehouseMap.get("hwxz").toString());
                storehouseMap.put("hwxz", hwxz);
            }
            //粮食品种
            if (storehouseMap.get("pz") != null) {
                String pz = wareHouseBasicInfoMapper.getEnum(storehouseMap.get("pz").toString());
                storehouseMap.put("pz", pz);
            }
            //仓房状态
            if (storehouseMap.get("storehouse_state") != null) {
                String storehouse_state = wareHouseBasicInfoMapper.getEnum(storehouseMap.get("storehouse_state").toString());
                storehouseMap.put("storehouse_state", storehouse_state);
            }
            //仓房类型
            if (storehouseMap.get("storehouse_type") != null) {
                String storehouse_type = wareHouseBasicInfoMapper.getEnum(storehouseMap.get("storehouse_type").toString());
                storehouseMap.put("storehouse_type", storehouse_type);
            }
            //保管员
            if (storehouseMap.get("unitid") != null && storehouseMap.get("storehouse_code") != null) {
                String keeperName = wareHouseBasicInfoMapper.getKeeperName(storehouseMap);
                storehouseMap.put("keeperName", keeperName);
            }
        }
        return storehouseMap;
    }
}
