package com.aidex.system.service.impl;

import com.aidex.common.constant.Constants;
import com.aidex.common.core.domain.BaseEntity;
import com.aidex.common.core.redis.RedisCache;
import com.aidex.common.core.service.BaseServiceImpl;
import com.aidex.framework.cache.ConfigUtils;
import com.aidex.system.domain.SysConfig;
import com.aidex.system.domain.SysWharf;
import com.aidex.system.domain.vo.SysRouteVo;
import com.aidex.system.mapper.SysConfigMapper;
import com.aidex.system.mapper.SysWharfMapper;
import com.aidex.system.service.SysWharfService;
import com.alibaba.fastjson2.JSONArray;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 码头管理Service业务层处理
 * @author ChenSir
 * @email 914769835
 * @date 2024-03-16
 */
@Service
@Transactional(readOnly = true)
public class SysWharfServiceImpl extends BaseServiceImpl<SysWharfMapper, SysWharf> implements SysWharfService {

    private static final Logger log = LoggerFactory.getLogger(SysWharfServiceImpl.class);

    @Autowired
    private SysConfigMapper sysConfigMapper;

    /**
     * 获取单条数据
     * @param sysWharf 码头管理
     * @return 码头管理
     */
    @Override
    public SysWharf get(SysWharf sysWharf) {
        SysWharf dto = super.get(sysWharf);
        return dto;
    }

    /**
     * 获取单条数据
     * @param id 码头管理id
     * @return 码头管理
     */
    @Override
    public SysWharf get(String id) {
        SysWharf dto = super.get(id);
        return dto;
    }

    /**
     * 查询码头管理列表
     * @param sysWharf 码头管理
     * @return 码头管理
     */
    @Override
    public List<SysWharf> findList(SysWharf sysWharf) {
		List<SysWharf> sysWharfList = super.findList(sysWharf);
        return sysWharfList;
    }

    /**
     * 分页查询码头管理列表
     * @param sysWharf 码头管理
     * @return 码头管理
     */
    @Override
    public PageInfo<SysWharf> findPage(SysWharf sysWharf) {
		PageInfo<SysWharf> page = super.findPage(sysWharf);
        return page;
    }

    /**
     * 保存码头管理
     * @param sysWharf
     * @return 结果
     */
    @Override
    public boolean save(SysWharf sysWharf) {
        return super.save(sysWharf);
    }

    /**
     * 删除码头管理信息
     * @param sysWharf
     * @return 结果
     */
    @Override
    public boolean remove(SysWharf sysWharf) {
        return super.remove(sysWharf);
    }

    /**
     * 批量删除码头管理
     * @param ids 需要删除的码头管理ID
     * @return 结果
     */
    @Transactional(readOnly = false)
    @Override
    public int deleteSysWharfByIds(String[] ids) {
        return mapper.deleteSysWharfByIds(ids, BaseEntity.DEL_FLAG_DELETE);
    }
    @Transactional(readOnly = false)
    @Override
    public int updateStatus(SysWharf sysWharf) {
        sysWharf.setStatus(BaseEntity.STATUS_NORMAL.equals(sysWharf.getStatus()) ? BaseEntity.STATUS_DISABLE : BaseEntity.STATUS_NORMAL);
        return mapper.updateStatus(sysWharf);
    }

    @Override
    public List<SysRouteVo> getRouteList() {
        List<SysRouteVo> SysRouteVoList  = new ArrayList<>();
        List<SysWharf> list = mapper.findList(new SysWharf());
        SysConfig sysConfigRoute = sysConfigMapper.findConfigByKey(Constants.ROUTE_STORE_PREFIX);
        JSONArray pathJson = JSONArray.parseArray(sysConfigRoute.getConfigValue());
        // 正向
        SysRouteVo sysRouteVo = new SysRouteVo();
        sysRouteVo.setFirstWharf(list.get(0));
        sysRouteVo.setLastWharf(list.get(list.size() - 1));
        sysRouteVo.setName(ConfigUtils.getConfigValueByKey("sys.route.name"));
        sysRouteVo.setFirstFlightTime(ConfigUtils.getConfigValueByKey("sys.route.start"));
        sysRouteVo.setLastFlightTime(ConfigUtils.getConfigValueByKey("sys.route.end"));
        sysRouteVo.setFare(ConfigUtils.getConfigValueByKey("sys.route.fare"));
        sysRouteVo.setPathList(pathJson);
        sysRouteVo.setSysWharfList(list);
        SysRouteVoList.add(sysRouteVo);
        // 反向
        SysRouteVo sysRouteVoRe = new SysRouteVo();
        sysRouteVoRe.setFirstWharf(list.get(list.size() - 1));
        sysRouteVoRe.setLastWharf(list.get(0));
        sysRouteVoRe.setName(ConfigUtils.getConfigValueByKey("sys.route.name"));
        sysRouteVoRe.setFirstFlightTime(ConfigUtils.getConfigValueByKey("sys.route.start"));
        sysRouteVoRe.setLastFlightTime(ConfigUtils.getConfigValueByKey("sys.route.end"));
        sysRouteVoRe.setFare(ConfigUtils.getConfigValueByKey("sys.route.fare"));
        Collections.reverse(pathJson);
        Collections.reverse(list);
        sysRouteVoRe.setSysWharfList(list);
        sysRouteVoRe.setPathList(pathJson);
        SysRouteVoList.add(sysRouteVoRe);

        return SysRouteVoList;
    }

}
