package com.ruoyi.basicData.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.basicData.domain.FleetCarManage;
import com.ruoyi.basicData.domain.FleetCompanyMsg;
import com.ruoyi.basicData.mapper.FleetCarManageMapper;
import com.ruoyi.basicData.mapper.FleetCompanyMsgMapper;
import com.ruoyi.basicData.service.IFleetCarManageService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.orderPlan.domain.Ftmsorderbillsattachs;
import com.ruoyi.orderPlan.domain.Ftmsorderbillscars;
import com.ruoyi.orderPlan.mapper.FtmsorderbillsattachsMapper;
import com.ruoyi.orderPlan.mapper.FtmsorderbillscarsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.*;

/**
 * 【车辆】Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-03-04
 */
@Service
public class FleetCarManageServiceImpl implements IFleetCarManageService
{
    @Autowired
    private FleetCarManageMapper fleetCarManageMapper;

    @Autowired
    private FtmsorderbillsattachsMapper ftmsorderbillsattachsMapper;

    @Autowired
    private FtmsorderbillscarsMapper ftmsorderbillscarsMapper;

    @Autowired
    private FleetCompanyMsgMapper fleetCompanyMsgMapper;

    /**
     * 查询【车辆】
     * 
     * @param id 【车辆】ID
     * @return 【车辆】
     */
    @Override
    public FleetCarManage selectFleetCarManageById(Long id)
    {
        return fleetCarManageMapper.selectFleetCarManageById(id);
    }

    @Override
    public Map<String, Object> selectFleetCarManageByIdMap(Long id) {
        Map<String, Object> map = new HashMap<>();
        FleetCarManage fleetCarManage = fleetCarManageMapper.selectFleetCarManageById(id);
        // 车队
        List<Long> companyId = new ArrayList<>();
        if (!StringUtils.isNull(fleetCarManage)) {
            if (StringUtils.isNotNull(fleetCarManage.getFleetCompanyId())) {
                companyId.add(fleetCarManage.getFleetCompanyId());
            }
        }
        List<FleetCompanyMsg> fleetCompanyMsgList= new ArrayList<>();
        List<Long> fleetCompanyMsgIdList = StringUtils.integerDeduplication(companyId);
        for (Long fleetCompanyMsgId : fleetCompanyMsgIdList) {
            FleetCompanyMsg fleetCompanyMsg = fleetCompanyMsgMapper.selectFleetCompanyMsgById(fleetCompanyMsgId);
            if (StringUtils.isNotNull(fleetCompanyMsg)) {
                fleetCompanyMsgList.add(fleetCompanyMsg);
            }
        }
        // 查询附件表数据
        Ftmsorderbillsattachs ftmsorderbillsattachs = new Ftmsorderbillsattachs();
        ftmsorderbillsattachs.setPId(fleetCarManage.getId());
        List<Ftmsorderbillsattachs> ftmsorderbillsattachsList = ftmsorderbillsattachsMapper.selectftmsorderbillsattachsList(ftmsorderbillsattachs);
        if (StringUtils.isNotEmpty(ftmsorderbillsattachsList)) {
            map.put("fTmsorderbillsattachs", ftmsorderbillsattachsList);
        }
        map.put("fleetCompanyMsg", fleetCompanyMsgList);
        map.put("fleetCarManage", fleetCarManage);
        return map;
    }

    /**
     * 查询【车辆】列表
     * 
     * @param fleetCarManage 【车辆】
     * @return 【车辆】
     */
    @Override
    public List<FleetCarManage> selectFleetCarManageList(FleetCarManage fleetCarManage)
    {
        return fleetCarManageMapper.selectFleetCarManageList(fleetCarManage);
    }

    @Override
    public List<Map<String, Object>> selectFleetCarManageListMap(FleetCarManage fleetCarManage) {
        return fleetCarManageMapper.selectFleetCarManageListMap(fleetCarManage);
    }

    /**
     * 新增【车辆】
     * 
     * @param fleetCarManage 【车辆】
     * @return 结果
     */
    @Override
    @Transactional
    public AjaxResult insertFleetCarManage(FleetCarManage fleetCarManage)
    {
        // 检验必填
        if(StringUtils.isNull(fleetCarManage.getFleetCompanyId()) ||
                StringUtils.isNull(fleetCarManage.getCarNum()) ){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("请填写必输项");
        }
        fleetCarManage.setBillStatus(6L);
        if(null!=fleetCarManage.getId() && !fleetCarManage.getId().equals("")){
            fleetCarManage.setUpdateBy(SecurityUtils.getUsername());
            fleetCarManage.setUpdateTime(new Date());
            fleetCarManageMapper.updateFleetCarManage(fleetCarManage);
            return AjaxResult.success();
        }else {
            fleetCarManage.setCreateBy(SecurityUtils.getUsername());
            fleetCarManage.setCreatTime(new Date());
            fleetCarManageMapper.insertFleetCarManage(fleetCarManage);
            return AjaxResult.success();
        }
    }

    @Override
    public AjaxResult insertFleetCarManageAdd(String fleetCarManage, String fTmsorderbillsattachs) {
        Long fPid = null;
        Map<String, Object> map = new HashMap<>();
        FleetCarManage fleetCarManage1 = JSONArray.parseObject(fleetCarManage, FleetCarManage.class);
        // 检验必填
        if(StringUtils.isNull(fleetCarManage1.getFleetCompanyId()) ||
                StringUtils.isNull(fleetCarManage1.getCarNum()) ){
            return AjaxResult.error("请填写必输项");
        }
        if (fleetCarManageMapper.checkCarNumUnique(fleetCarManage1.getCarNum()) > 0 && StringUtils.isNull(fleetCarManage1.getId())) {
            return AjaxResult.error("车牌号已存在，请确认");
        }
        fleetCarManage1.setBillStatus(6L);
        if(null!=fleetCarManage1.getId() && !fleetCarManage1.getId().equals("")){
            fPid = fleetCarManage1.getId();
            fleetCarManage1.setUpdateBy(SecurityUtils.getUsername());
            fleetCarManage1.setUpdateTime(new Date());
            fleetCarManageMapper.updateFleetCarManage(fleetCarManage1);
            ftmsorderbillsattachsMapper.deleteftmsorderbillsattachsByPId(fPid);
        }else {
            fleetCarManage1.setCreateBy(SecurityUtils.getUsername());
            fleetCarManage1.setCreatTime(new Date());
            fleetCarManageMapper.insertFleetCarManage(fleetCarManage1);
            fPid = fleetCarManage1.getId();
        }
        //  附件添加
        if (StringUtils.isNotNull(fTmsorderbillsattachs) && !"[]".equals(fTmsorderbillsattachs)) {
            JSONArray jsonDrArray = JSONArray.parseArray(fTmsorderbillsattachs);
            List<Ftmsorderbillsattachs> ftmsorderbillsattachsList = JSONObject.parseArray(jsonDrArray.toJSONString(), Ftmsorderbillsattachs.class);
            for (Ftmsorderbillsattachs ftmsorderbillsattachs : ftmsorderbillsattachsList) {
                ftmsorderbillsattachs.setPId(fPid);
                ftmsorderbillsattachs.setOrgId(fPid);
                ftmsorderbillsattachs.setCreateBy(SecurityUtils.getUsername());
                ftmsorderbillsattachs.setCreateTime(new Date());
                ftmsorderbillsattachsMapper.insertftmsorderbillsattachs(ftmsorderbillsattachs);
            }
        }
        map.put("fleetCarManage", fleetCarManage1);
        return AjaxResult.success("成功", map);
    }

    /**
     * 修改【车辆】
     * 
     * @param fleetCarManage 【车辆】
     * @return 结果
     */
    @Override
    public int updateFleetCarManage(FleetCarManage fleetCarManage)
    {
        fleetCarManage.setUpdateTime(DateUtils.getNowDate());
        return fleetCarManageMapper.updateFleetCarManage(fleetCarManage);
    }

    /**
     * 批量删除【车辆】
     * 
     * @param ids 需要删除的【车辆】ID
     * @return 结果
     */
    @Override
    public AjaxResult deleteFleetCarManageByIds(Long[] ids)
    {
        for (Long id : ids) {
            Ftmsorderbillscars ftmsorderbillscars = new Ftmsorderbillscars();
            ftmsorderbillscars.setCarcorPid(id);
            if (ftmsorderbillscarsMapper.selectftmsorderbillscarsCount(ftmsorderbillscars) > 0) {
                return AjaxResult.error("该车辆已存在状态跟踪中，无法删除");
            }
        }
        fleetCarManageMapper.deleteFleetCarManageByIds(ids);
        return AjaxResult.success();
    }

    /**
     * 删除【车辆】信息
     * 
     * @param id 【车辆】ID
     * @return 结果
     */
    @Override
    public int deleteFleetCarManageById(Long id)
    {
        return fleetCarManageMapper.deleteFleetCarManageById(id);
    }

    /**
     *  下拉列表查询【车辆】列表id name
     * @param fleetCarManage    条件
     * @return  结果
     */
    @Override
    public List<FleetCarManage> selectFleetCarManageIdNameList(FleetCarManage fleetCarManage) {
        return fleetCarManageMapper.selectFleetCarManageIdNameList(fleetCarManage);
    }
}
