package com.ruoyi.shipping.service.impl;

import java.util.*;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.shipping.domain.TCntrtracking;
import com.ruoyi.shipping.domain.TConditionAccessory;
import com.ruoyi.shipping.mapper.*;
import com.ruoyi.system.mapper.SysDictDataMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.shipping.domain.TCntrno;
import com.ruoyi.shipping.service.ITCntrnoService;

/**
 * 集装箱号Service业务层处理
 *
 * @author ruoyi
 * @date 2021-03-22
 */
@Service
public class TCntrnoServiceImpl implements ITCntrnoService
{
    @Autowired
    private TCntrnoMapper tCntrnoMapper;
    @Autowired
    private TCntrtrackingMapper tCntrtrackingMapper;
    @Autowired
    private TCntrMapper tCntrMapper;
    @Autowired
    private SysDictDataMapper sysDictDataMapper;
    @Autowired
    private TAddressMapper tAddressMapper;
    @Autowired
    private TConditionAccessoryMapper tConditionAccessoryMapper;

    /**
     * 查询集装箱号
     *
     * @param fId 集装箱号ID
     * @return 集装箱号
     */
    @Override
    public TCntrno selectTCntrnoById(Long fId)
    {
        return tCntrnoMapper.selectTCntrnoById(fId);
    }

    /**
     * 查询集装箱号列表
     *
     * @param tCntrno 集装箱号
     * @return 集装箱号
     */
    @Override
    public List<TCntrno> selectTCntrnoList(TCntrno tCntrno)
    {
        return tCntrnoMapper.selectTCntrnoList(tCntrno);
    }

    /**
     * 新增集装箱号
     *
     * @param tCntrno 集装箱号
     * @return 结果
     */
    @Override
    public int insertTCntrno(TCntrno tCntrno)
    {
        tCntrno.setCreateTime(DateUtils.getNowDate());
        tCntrno.setCreateBy(SecurityUtils.getUsername());
        tCntrno.setfUpdatetime(DateUtils.getNowDate());
        tCntrno.setfStatus("T");
        return tCntrnoMapper.insertTCntrno(tCntrno);
    }

    /**
     * 修改集装箱号
     *
     * @param tCntrno 集装箱号
     * @return 结果
     */
    @Override
    public int updateTCntrno(List<TCntrno> tCntrno)
    {
        int update = 0;
        if (CollectionUtils.isNotEmpty(tCntrno)){
            for (TCntrno cntrno : tCntrno) {
                String remark = cntrno.getRemark();
                cntrno.setUpdateTime(DateUtils.getNowDate());
                cntrno.setUpdateBy(SecurityUtils.getUsername());
                //最新时间
                if (cntrno.getFoldtime() == null){
                    cntrno.setfUpdatetime(DateUtils.getNowDate());
                }else {
                    cntrno.setfUpdatetime(cntrno.getFoldtime());
                }
                //调入地点
                if (StringUtils.isNotEmpty(cntrno.getFoldSite())){
                    cntrno.setfUpdateaddress(cntrno.getFoldSite());
                }
                //调入空重
                if (StringUtils.isNotEmpty(cntrno.getFoldFUpdateef())){
                    cntrno.setfUpdateef(cntrno.getFoldFUpdateef());
                }
                //调入状态
                if (StringUtils.isNotEmpty(cntrno.getFoldFCntrstatus())){
                    cntrno.setfCntrstatus(cntrno.getFoldFCntrstatus());
                }
                if (StringUtils.isNotEmpty(remark)){
                    cntrno.setRemark(null);
                }
                //更改t_cntro表信息
                update = tCntrnoMapper.updateTCntrno((cntrno));
                //保存信息
                TCntrno cntrnoById = tCntrnoMapper.selectTCntrnoById(cntrno.getfId());
                if (cntrnoById.getfStatus().equals("T") ||cntrnoById.getfStatus().equals("正常")){
                    cntrnoById.setfStatus("T");
                }else {
                    cntrnoById.setfStatus("F");
                }
                if (StringUtils.isNotEmpty(cntrno.getfMblno())){//提单号
                    cntrnoById.setfMblno(cntrno.getfMblno());
                }
                if (StringUtils.isNotEmpty(cntrno.getfVoy())){//航次
                    cntrnoById.setfVoy(cntrno.getfVoy());
                }
                if (StringUtils.isNotEmpty(cntrno.getfVsl())){//船名
                    cntrnoById.setfVsl(cntrno.getfVsl());
                }
                if (StringUtils.isNotEmpty(remark)){
                    cntrnoById.setRemark(remark);
                }
                TCntrtracking setfield = setfield(cntrnoById);
                tCntrtrackingMapper.insertTCntrtracking(setfield);
                if (CollectionUtils.isNotEmpty(cntrno.getAccessoryList())){
                    for (TConditionAccessory tConditionAccessory : cntrno.getAccessoryList()) {
                        tConditionAccessory.setfPid(setfield.getfId());
                        tConditionAccessoryMapper.insertTConditionAccessory(tConditionAccessory);
                    }
                }
            }
        }
        return update;
    }

    @Override
    public int updateTCntrnoMessage(TCntrno tCntrno) {
        tCntrno.setUpdateTime(DateUtils.getNowDate());
        tCntrno.setUpdateBy(SecurityUtils.getUsername());
        tCntrno.setfUpdatetime(DateUtils.getNowDate());
        if (tCntrno.getfStatus().equals("T") ||tCntrno.getfStatus().equals("正常")){
            tCntrno.setfStatus("T");
        }else {
            tCntrno.setfStatus("F");
        }
        TCntrtracking setfield = setfield(tCntrno);
        tCntrtrackingMapper.insertTCntrtracking(setfield);
        //更改t_cntro表信息
        return tCntrnoMapper.updateTCntrno((tCntrno));

    }

    /**
     * 批量删除集装箱号
     *
     * @param fIds 需要删除的集装箱号ID
     * @return 结果
     */
    @Override
    public int deleteTCntrnoByIds(Long[] fIds)
    {
        return tCntrnoMapper.deleteTCntrnoByIds(fIds);
    }

    /**
     * 删除集装箱号信息
     *
     * @param fId 集装箱号ID
     * @return 结果
     */
    @Override
    public int deleteTCntrnoById(Long fId)
    {
        return tCntrnoMapper.deleteTCntrnoById(fId);
    }

    /**
     * 批量更新数据状态将正常状态改为停用
     * @param fIds
     * @return
     */
    @Override
    public int updateTcntrnoStatus(Long[] fIds) {
        return tCntrnoMapper.updateTcntrnoStatus(fIds);
    }

    @Override
    public List<TCntrno> selectTcntrnoMessage(TCntrno tCntrno) {
        return tCntrnoMapper.selectTcntrnoMessage(tCntrno,null,null,null,null,null);
    }

    @Override
    public List<Map<String, Object>> getTCntrnoMessage(TCntrno tCntrno) {
        StringBuffer stringBuffer = new StringBuffer();
        List<String> list = tCntrMapper.selectTCntrFNo();
        String sql = null;
        if (CollectionUtils.isNotEmpty(list)){
            for (String s : list) {
                stringBuffer.append("MAX( CASE WHEN tt.f_no = '"+s);
                stringBuffer.append("' THEN tt.typeidCount ELSE 0 END ) AS '" +s +"',");
            }
            sql = stringBuffer.subSequence(0,stringBuffer.length() -1).toString();
        }
        return tCntrnoMapper.getTCntrnoMessage(sql,tCntrno);
    }

    @Override
    public List<TCntrno> getTCntrnoDistribute(TCntrno tCntrno) {
        //箱类型不为空查箱类型id
        String address = null;//地点
        String typeidName = null;//箱类型
        String cntrstatusName = null;//箱状态
        String updateEFName = null;//空重
        if (StringUtils.isNotEmpty(tCntrno.getAddressName())){
            address = tCntrno.getAddressName();
        }
        if (StringUtils.isNotEmpty(tCntrno.getTypeidName())){
            typeidName = tCntrno.getTypeidName();
        }
        //箱状态不为空查箱状态
        if (StringUtils.isNotEmpty(tCntrno.getCntrstatusName())){
            cntrstatusName = tCntrno.getCntrstatusName();
        }
        //箱空重部位空查空重
        if (StringUtils.isNotEmpty(tCntrno.getUpdateEFName())){
            updateEFName = tCntrno.getUpdateEFName();
        }
        return tCntrnoMapper.selectTcntrnoMessage(tCntrno,address,typeidName,cntrstatusName,updateEFName,tCntrno.getCntrsize());
    }

    @Override
    public List<String> getTCntrnoChange(TCntrno tCntrno) {
        List<String> key = new ArrayList<>();
        key.add("地点");
        List<String> list = tCntrMapper.selectTCntrFNo();
        key.addAll(list);
        key.add("空");
        key.add("重");
        key.add("好");
        key.add("坏");
        return key;
    }

    @Override
    public String selectTCntrnoFNo(TCntrno tCntrno) {
        TCntrno cntrno = tCntrnoMapper.selectTCntrnoFNo(tCntrno);
        if (StringUtils.isNotNull(cntrno) && !cntrno.getfId().equals(tCntrno.getfId())) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * set字段
     * @param tCntrno
     * @return
     */
    public TCntrtracking  setfield(TCntrno tCntrno){
        TCntrtracking tCntrtracking = new TCntrtracking();
        BeanUtils.copyProperties(tCntrno,tCntrtracking);
        tCntrtracking.setfId(null);
        return tCntrtracking;
    }
    public List<String> GetMapKey(List<Map<String, Object>> listResult)
    {
        if ((listResult != null) && (!listResult.isEmpty()))
        {
            List listKey = new ArrayList();
            Map mapResult = (Map)listResult.get(0);
            Set mapKeySet = mapResult.keySet();
            String listHead = "";
            Iterator iteratorKey = mapKeySet.iterator();
            while (iteratorKey.hasNext()) {
                listHead = iteratorKey.next().toString();
                listKey.add(listHead);
            }
            return listKey;
        }
        return null;
    }
}
