/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.biz.api.DictOptions.service;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;
import vip.xiaonuo.auth.modular.login.enums.AuthDeviceTypeEnum;
import vip.xiaonuo.biz.api.DictOptions.entity.MiNiDictOptionsVo;
import vip.xiaonuo.biz.modular.depotGrain.entity.DepotGrain;
import vip.xiaonuo.biz.modular.depotGrain.param.DepotGrainPageParam;
import vip.xiaonuo.biz.modular.depotGrain.service.DepotGrainService;
import vip.xiaonuo.biz.api.DictOptions.entity.DictOptionsVo;
import vip.xiaonuo.biz.api.DictOptions.param.DictOptionsParam;
import vip.xiaonuo.biz.modular.depotGrainShed.entity.DepotGrainShed;
import vip.xiaonuo.biz.modular.depotGrainShed.param.DepotGrainShedPageParam;
import vip.xiaonuo.biz.modular.depotGrainShed.service.DepotGrainShedService;
import vip.xiaonuo.biz.modular.depotZonePrice.entity.DepotZonePrice;
import vip.xiaonuo.biz.modular.depotZonePrice.service.DepotZonePriceService;
import vip.xiaonuo.dev.modular.dict.entity.DevDict;
import vip.xiaonuo.dev.modular.dict.param.DevDictTreeParam;
import vip.xiaonuo.dev.modular.dict.service.DevDictService;
import vip.xiaonuo.sys.modular.role.entity.SysRole;
import vip.xiaonuo.sys.modular.role.service.SysRoleService;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

/**
 * 下拉框Service接口
 *
 * @author xiang
 * @date 2023/03/07 14:48
 **/
@Service
public class DictOptionsService {

    @Resource
    DepotGrainService depotGrainService;
    @Resource
    DepotGrainShedService depotGrainShedService;
    @Resource
    DevDictService devDictService;
    @Resource
    DepotZonePriceService zonePriceService;
    @Resource
    SysRoleService sysRoleService;

    /**
     * 获取下拉框
     *
     * @author xiang
     * @date 2023/03/07 14:48
     **/
    public Object dictOptions(DictOptionsParam param, String type) {
        if (Objects.equals(param.getDictValue(), "tree"))
            return devDictService.tree(new DevDictTreeParam("BIZ"));
        List<DictOptionsVo> vos = dictOptions(param);
        if (AuthDeviceTypeEnum.MINI.getValue().equals(type)) {
            List<MiNiDictOptionsVo> miNiVos = new LinkedList<>();
            vos.forEach(vo -> {
                miNiVos.add(new MiNiDictOptionsVo(vo.getLabel(), vo.getValue()));
            });
            return miNiVos;
        }
        return vos;
    }

    /**
     * 获取下拉框
     *
     * @author xiang
     * @date 2023/03/07 14:48
     **/
    public List<DictOptionsVo> dictOptions(DictOptionsParam param) {
        List<DictOptionsVo> vos = new LinkedList<>();
        //粮库列表下拉框
        if ("粮库".equals(param.getDictValue())) {
            List<DepotGrain> list = depotGrainService.lstOrgGrep(new DepotGrainPageParam());
            list.forEach(one -> {
                vos.add(new DictOptionsVo(one.getDepotGrainName(), one.getDepotId()));
            });
        } else if ("库位".equals(param.getDictValue()) && Strings.isNotBlank(param.getDepotId())) {
            List<DepotGrainShed> list = depotGrainShedService.lst(new DepotGrainShedPageParam(param.getDepotId(), null));
            list.forEach(one -> {
                vos.add(new DictOptionsVo(one.getShedName(), one.getShedName()));
            });
        } else if ("主流价格".equals(param.getDictValue()) && Strings.isNotBlank(param.getGrainGroup())) {
            List<DepotZonePrice> list = zonePriceService.lst(new DepotZonePrice(param.getGrainGroup()));
            list.forEach(one -> {
                vos.add(new DictOptionsVo(StringUtils.join(one.getZoneName(), "：", one.getZonePrice(), " (元/吨)"), one.getId()));
            });
        } else if ("业务角色".equals(param.getDictValue())) {
            List<SysRole> list = sysRoleService.roleBizLst();
            list.forEach(one -> {
                vos.add(new DictOptionsVo(one.getName(), one.getId()));
            });
        } else if (Strings.isNotEmpty(param.getDictValue())) {
            List<DevDict> list = devDictService.listSon(param.getDictValue());
            list.stream().map(DevDict::getDictValue).distinct().forEach(one -> {
                vos.add(new DictOptionsVo(one, one));
            });
        }
        return vos;
    }
}
