package com.ruoyi.shipping.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.shipping.domain.TCntr;
import com.ruoyi.shipping.domain.TCtnpriceItems;
import com.ruoyi.shipping.mapper.TCntrMapper;
import com.ruoyi.shipping.mapper.TCtnpriceItemsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.shipping.mapper.TCtnpriceMapper;
import com.ruoyi.shipping.domain.TCtnprice;
import com.ruoyi.shipping.service.ITCtnpriceService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * 海运运价Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-04-06
 */
@Service
public class TCtnpriceServiceImpl implements ITCtnpriceService 
{
    @Autowired
    private TCtnpriceMapper tCtnpriceMapper;
    @Autowired
    private TCtnpriceItemsMapper tCtnpriceItemsMapper;
    @Autowired
    private TCntrMapper tCntrMapper;

    /**
     * 查询海运运价
     * 
     * @param fId 海运运价ID
     * @return 海运运价
     */
    @Override
    public TCtnprice selectTCtnpriceById(Long fId)
    {
        return tCtnpriceMapper.selectTCtnpriceById(fId);
    }

    @Override
    public AjaxResult selectTCtnprice(Long fId) {
        Map<String, Object> map = new HashMap<>();
        TCtnprice tCtnprice = tCtnpriceMapper.selectTCtnpriceById(fId);
        TCtnpriceItems tCtnpriceItems = new TCtnpriceItems();
        tCtnpriceItems.setfPid(fId);
        List<TCtnpriceItems> ctnpriceItems = tCtnpriceItemsMapper.selectTCtnpriceItemsList(tCtnpriceItems);
        map.put("tCtnprice",tCtnprice);
        map.put("ctnpriceItems",ctnpriceItems);
        return AjaxResult.success("成功",map);
    }

    /**
     * 查询海运运价列表
     * 
     * @param tCtnprice 海运运价
     * @return 海运运价
     */
    @Override
    public List<TCtnprice> selectTCtnpriceList(TCtnprice tCtnprice)
    {
        return tCtnpriceMapper.selectTCtnpriceList(tCtnprice);
    }

    /**
     * 新增海运运价
     * 
     * @param tCtnprice 海运运价
     * @return 结果
     */
    @Override
    public int insertTCtnprice(TCtnprice tCtnprice)
    {
        tCtnprice.setCreateTime(DateUtils.getNowDate());
        tCtnprice.setCreateBy(SecurityUtils.getUsername());
        return tCtnpriceMapper.insertTCtnprice(tCtnprice);
    }

    @Override
    @Transactional
    public AjaxResult insertTCtnpriceNew(String tCtnprice, String tCtnpriceItems) {
        TCtnprice ctnprice = JSONArray.parseObject(tCtnprice, TCtnprice.class);
        if (ctnprice.getfPortofloadid() == null){
            return AjaxResult.error("装货港不能为空");
        }
        if (ctnprice.getfDistinationid() == null){
            return AjaxResult.error("目的港不能为空");
        }
       /* if (ctnprice.getfPortoftransshipment() == null){
            return AjaxResult.error("中转港不能为空");
        }*/
        /*if (ctnprice.getfEtd() == null){
            return AjaxResult.error("预计开船日期不能为空");
        }
        if (ctnprice.getfEta() == null){
            return AjaxResult.error("预计到达日期不能为空");
        }*/
        if (ctnprice.getfBegindate() == null){
            return AjaxResult.error("有效期止不能为空");
        }
        List<TCtnprice> tCtnpriceList = tCtnpriceMapper.selectMessage(ctnprice);
        if (CollectionUtils.isNotEmpty(tCtnpriceList)){
            for (TCtnprice Price : tCtnpriceList) {
                if (ctnprice.getfId() != null && ctnprice.getfId() != Price.getfId()){
                    return AjaxResult.error("当前起始港口的有效期内的运价条目重复,请确认");
                }else if (ctnprice.getfId() == null){
                    return AjaxResult.error("当前起始港口的有效期内的运价条目重复,请确认");
                }
            }
        }
        List<TCtnpriceItems> ctnpriceItems = new ArrayList<>();
        if (ctnprice.getfId() == null){
            ctnprice.setCreateTime(DateUtils.getNowDate());
            ctnprice.setCreateBy(SecurityUtils.getUsername());
            /*if (ctnprice.getfDays()==null){
                long dateDay = DateUtils.getDateDay(ctnprice.getfEta(),ctnprice.getfEtd());
                ctnprice.setfDays(dateDay);
            }*/
            int i = tCtnpriceMapper.insertTCtnprice(ctnprice);
            JSONArray warehouseJSON = JSONArray.parseArray(tCtnpriceItems);
            ctnpriceItems = JSONObject.parseArray(warehouseJSON.toJSONString(), TCtnpriceItems.class);
            if (CollectionUtils.isNotEmpty(ctnpriceItems)){
                for (TCtnpriceItems ctnpriceItem : ctnpriceItems) {
                    List<TCtnpriceItems> ctnpriceItemsList = tCtnpriceItemsMapper.selectItemMeaasge(ctnprice.getfId(), ctnpriceItem.getfCntrid());
                    if (CollectionUtils.isNotEmpty(ctnpriceItemsList)){
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        TCntr cntr = tCntrMapper.selectTCntrById(ctnpriceItem.getfCntrid());
                        return AjaxResult.error("运价中集装箱编号"+cntr.getfNo()+"重复，请确认");
                    }
                    ctnpriceItem.setfPid(ctnprice.getfId());
                    ctnpriceItem.setCreateTime(DateUtils.getNowDate());
                    ctnpriceItem.setCreateBy(SecurityUtils.getUsername());
                    int items = tCtnpriceItemsMapper.insertTCtnpriceItems(ctnpriceItem);
                }
            }
        }else {
            ctnprice.setUpdateTime(DateUtils.getNowDate());
            ctnprice.setUpdateBy(SecurityUtils.getUsername());
            /*if (ctnprice.getfDays()==null){
                long dateDay = DateUtils.getDateDay(ctnprice.getfEta(),ctnprice.getfEtd());
                ctnprice.setfDays(dateDay);
            }*/
            if (ctnprice.getfStatus().equals("T") ||ctnprice.getfStatus().equals("正常")){
                ctnprice.setfStatus("T");
            }else {
                ctnprice.setfStatus("F");
            }
            int updateTCtnprice = tCtnpriceMapper.updateTCtnprice(ctnprice);
            JSONArray warehouseJSON = JSONArray.parseArray(tCtnpriceItems);
            ctnpriceItems = JSONObject.parseArray(warehouseJSON.toJSONString(), TCtnpriceItems.class);
            if (CollectionUtils.isNotEmpty(ctnpriceItems)){
                tCtnpriceItemsMapper.deleteTCtnpriceItemsByfPid(ctnprice.getfId());
                for (TCtnpriceItems ctnpriceItem : ctnpriceItems) {
                    List<TCtnpriceItems> ctnpriceItemsList = tCtnpriceItemsMapper.selectItemMeaasge(ctnprice.getfId(), ctnpriceItem.getfCntrid());
                    if (CollectionUtils.isNotEmpty(ctnpriceItemsList)){
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        TCntr cntr = tCntrMapper.selectTCntrById(ctnpriceItem.getfCntrid());
                        return AjaxResult.error("运价中集装箱编号"+cntr.getfNo()+"重复，请确认");
                    }
                    ctnpriceItem.setfPid(ctnprice.getfId());
                    ctnpriceItem.setCreateTime(DateUtils.getNowDate());
                    ctnpriceItem.setCreateBy(SecurityUtils.getUsername());
                    if (ctnpriceItem.getfStatus().equals("T") ||ctnpriceItem.getfStatus().equals("正常")){
                        ctnpriceItem.setfStatus("T");
                    }else {
                        ctnpriceItem.setfStatus("F");
                    }
                    int items = tCtnpriceItemsMapper.insertTCtnpriceItems(ctnpriceItem);
                }
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("ctnprice",ctnprice);
        map.put("ctnpriceItems",ctnpriceItems);
        return AjaxResult.success("成功",map);
    }

    /**
     * 修改海运运价
     * 
     * @param tCtnprice 海运运价
     * @return 结果
     */
    @Override
    public int updateTCtnprice(TCtnprice tCtnprice)
    {
        tCtnprice.setUpdateTime(DateUtils.getNowDate());
        tCtnprice.setUpdateBy(SecurityUtils.getUsername());
        return tCtnpriceMapper.updateTCtnprice(tCtnprice);
    }

    /**
     * 批量删除海运运价
     * 
     * @param fIds 需要删除的海运运价ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteTCtnpriceByIds(Long[] fIds)
    {
        int i;
        i = tCtnpriceMapper.deleteTCtnpriceByIds(fIds);
        i = tCtnpriceItemsMapper.deleteTCtnpriceItemsByfPids(fIds);
        return i;
    }

    /**
     * 删除海运运价信息
     * 
     * @param fId 海运运价ID
     * @return 结果
     */
    @Override
    public int deleteTCtnpriceById(Long fId)
    {
        return tCtnpriceMapper.deleteTCtnpriceById(fId);
    }

    @Override
    public List<TCtnprice> freightSelect(TCtnprice tCtnprice) {
        List<TCtnprice> tCtnprices = tCtnpriceMapper.selectTCtnpriceList(tCtnprice);
        if (CollectionUtils.isNotEmpty(tCtnprices)){
            for (TCtnprice ctnprice : tCtnprices) {
                TCtnpriceItems items = new TCtnpriceItems();
                items.setfPid(ctnprice.getfId());
                List<TCtnpriceItems> tCtnpriceItems = tCtnpriceItemsMapper.selectTCtnpriceItemsList(items);
                ctnprice.settCtnpriceItemsList(tCtnpriceItems);
            }
        }
        return tCtnprices;
    }

}
