package com.ruoyi.orderManagement.statusTracking.service.impl;

import java.util.*;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.basicData.domain.TCorps;
import com.ruoyi.basicData.domain.TFees;
import com.ruoyi.basicData.domain.TGoods;
import com.ruoyi.basicData.mapper.TCorpsMapper;
import com.ruoyi.basicData.mapper.TFeesMapper;
import com.ruoyi.basicData.mapper.TGoodsMapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.entity.TWarehouse;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.orderManagement.BasicData.domain.FleetCompanyMsg;
import com.ruoyi.orderManagement.domain.Ftmsorderbills;
import com.ruoyi.orderManagement.domain.Ftmsorderbillsattachs;
import com.ruoyi.orderManagement.domain.Ftmsorderbillscars;
import com.ruoyi.orderManagement.mapper.FtmsorderbillsMapper;
import com.ruoyi.orderManagement.mapper.FtmsorderbillscarsMapper;
import com.ruoyi.orderManagement.statusTracking.domain.Ftmsorderbillsfees;
import com.ruoyi.orderManagement.statusTracking.mapper.FtmsorderbillsfeesMapper;
import com.ruoyi.orderManagement.statusTracking.service.IftmsorderbillsfeesService;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.warehouseBusiness.domain.TWarehousebillsfees;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 订单费用Service业务层处理
 *
 * @author ruoyi
 * @date 2021-02-22
 */
@Service
public class FtmsorderbillsfeesServiceImpl implements IftmsorderbillsfeesService {
    @Autowired
    private FtmsorderbillsfeesMapper fTmsorderbillsfeesMapper;

    @Autowired
    private FtmsorderbillscarsMapper ftmsorderbillscarsMapper;

    @Autowired
    private FtmsorderbillsMapper ftmsorderbillsMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private TGoodsMapper tGoodsMapper;

    @Autowired
    private TFeesMapper tFeesMapper;

    @Autowired
    private TCorpsMapper tCorpsMapper;



    /**
     * 查询车队费用
     *
     * @param fId 车队费用ID
     * @return 车队费用
     */
    @Override
    public Ftmsorderbillsfees selectFTmsorderbillsfeesById(Long fId) {
        return fTmsorderbillsfeesMapper.selectFTmsorderbillsfeesById(fId);
    }

    @Override
    public Map<String, Object> selectFTmsorderbillsfeesByIdMap(Long id) {
        Map<String, Object> map = new HashMap<>();
        // 客户表
        List<Long> corpsId = new ArrayList<>();
        // 用户表
        List<SysUser> sysUserList = new ArrayList<>();
        // 费用
        List<Long> feesId = new ArrayList<>();
        // 商品
        List<Long> goodsId = new ArrayList<>();
        // cars
        Ftmsorderbillscars ftmsorderbillscars = ftmsorderbillscarsMapper.selectftmsorderbillscarsById(id);
        // 主表
        Ftmsorderbills ftmsorderbills = ftmsorderbillsMapper.selectftmsorderbillsById(ftmsorderbillscars.getPId());
        if (!StringUtils.isNull(ftmsorderbills)) {
            if (StringUtils.isNotNull(ftmsorderbills.getCorpId())) {
                corpsId.add(ftmsorderbills.getCorpId());
            }
            if (StringUtils.isNotNull(ftmsorderbills.getGoodsId())) {
                goodsId.add(ftmsorderbills.getGoodsId());
            }
            // 查询制单人
            SysUser logUser = sysUserMapper.selectUserByUserName(ftmsorderbills.getCreateBy());
            if (StringUtils.isNotNull(logUser)) {
                SysDept sysDept = sysDeptMapper.selectDeptById(logUser.getDeptId());
                sysUserList.add(logUser);
                map.put("dept", sysDept);
            }
        }
        // 费用信息
        Ftmsorderbillsfees ftmsorderbillsfees = new Ftmsorderbillsfees();
        //ftmsorderbillsfees.setFPid(id);
        ftmsorderbillsfees.setfPid(id);
        List<Ftmsorderbillsfees> ftmsorderbillsfeesList= fTmsorderbillsfeesMapper.selectFTmsorderbillsfeesList(ftmsorderbillsfees);
        if (StringUtils.isNotEmpty(ftmsorderbillsfeesList)) {
            for (Ftmsorderbillsfees fees : ftmsorderbillsfeesList) {
                //feesId.add(fees.getFFeeid());
                feesId.add(fees.getfFeeid());
            }
        }
        // 客户
        List<TCorps> corpsList = new ArrayList<>();
        List<Long> corpsIdList = StringUtils.integerDeduplication(corpsId);
        for (Long corpId : corpsIdList) {
            TCorps corps = tCorpsMapper.selectTCorpsById(corpId);
            if (StringUtils.isNotNull(corps)) {
                corpsList.add(corps);
            }
        }
        // 商品
        List<TGoods> goodsList = new ArrayList<>();
        List<Long> goodsIdList = StringUtils.integerDeduplication(goodsId);
        for (Long goods : goodsIdList) {
            TGoods tGoods = tGoodsMapper.selectTGoodsById(goods);
            if (StringUtils.isNotNull(tGoods)) {
                goodsList.add(tGoods);
            }
        }
        // 费用
        List<TFees> feesList = new ArrayList<>();
        List<Long> longList = StringUtils.integerDeduplication(feesId);
        for (Long fees : longList) {
            TFees tFees = tFeesMapper.selectTFeesById(fees);
            if (StringUtils.isNotNull(tFees)) {
                feesList.add(tFees);
            }
        }
        map.put("corps", corpsList);
        map.put("goodsList", goodsList);
        map.put("feesList", feesList);
        map.put("ftmsorderbills", ftmsorderbills); // 主表
        map.put("ftmsorderbillsfees", ftmsorderbillsfeesList); // 费用
        return map;
    }

    @Override
    public AjaxResult insertFTmsorderbillsfeesAdd(String Ftmsorderbillsfees) {
        Map<String, Object> map = new HashMap<>();
        Long id =null;
        //  费用添加
        if (StringUtils.isNotNull(Ftmsorderbillsfees) && !"[]".equals(Ftmsorderbillsfees)) {
            JSONArray jsonDrArray = JSONArray.parseArray(Ftmsorderbillsfees);
            List<Ftmsorderbillsfees> ftmsorderbillsfeesList = JSONObject.parseArray(jsonDrArray.toJSONString(), Ftmsorderbillsfees.class);
            for (Ftmsorderbillsfees ftmsorderbillsfees : ftmsorderbillsfeesList) {
                // 只处理新追加的数据
                if(StringUtils.isNull(ftmsorderbillsfees.getfId())){
                    ftmsorderbillsfees.setfPid(ftmsorderbillsfees.getfPid());
                    ftmsorderbillsfees.setActId(1080L); // 追加费用
                    ftmsorderbillsfees.setOrgId(ftmsorderbillsfees.getfPid());
                    ftmsorderbillsfees.setCreateBy(SecurityUtils.getUsername());
                    ftmsorderbillsfees.setCreateTime(new Date());
                    fTmsorderbillsfeesMapper.insertFTmsorderbillsfees(ftmsorderbillsfees);
                }
                id= ftmsorderbillsfees.getfPid();
            }
        }
        Ftmsorderbillsfees ftmsorderbillsfees = new Ftmsorderbillsfees();
        ftmsorderbillsfees.setfPid(id);
        List<Ftmsorderbillsfees> ftmsorderbillsfeesListNew = fTmsorderbillsfeesMapper.selectFTmsorderbillsfeesList(ftmsorderbillsfees);
        map.put("ftmsorderbillsfeesList",ftmsorderbillsfeesListNew);
        return AjaxResult.success("成功", map);
    }

    /**
     * 查询车队费用列表
     *
     * @param fTmsorderbillsfees 车队费用
     * @return 车队费用
     */
    @Override
    public List<Ftmsorderbillsfees> selectFTmsorderbillsfeesList(Ftmsorderbillsfees fTmsorderbillsfees) {
        return fTmsorderbillsfeesMapper.selectFTmsorderbillsfeesList(fTmsorderbillsfees);
    }

    /**
     * 新增车队费用
     *
     * @param fTmsorderbillsfees 车队费用
     * @return 结果
     */
    @Override
    public int insertFTmsorderbillsfees(Ftmsorderbillsfees fTmsorderbillsfees) {
        fTmsorderbillsfees.setCreateTime(DateUtils.getNowDate());
        return fTmsorderbillsfeesMapper.insertFTmsorderbillsfees(fTmsorderbillsfees);
    }

    /**
     * 修改车队费用
     *
     * @param fTmsorderbillsfees 车队费用
     * @return 结果
     */
    @Override
    public int updateFTmsorderbillsfees(Ftmsorderbillsfees fTmsorderbillsfees) {
        fTmsorderbillsfees.setUpdateTime(DateUtils.getNowDate());
        return fTmsorderbillsfeesMapper.updateFTmsorderbillsfees(fTmsorderbillsfees);
    }

    /**
     * 批量删除车队费用
     *
     * @param fIds 需要删除的车队费用ID
     * @return 结果
     */
    @Override
    public int deleteFTmsorderbillsfeesByIds(Long[] fIds) {
        return fTmsorderbillsfeesMapper.deleteFTmsorderbillsfeesByIds(fIds);
    }

    /**
     * 删除车队费用信息
     *
     * @param fId 车队费用ID
     * @return 结果
     */
    @Override
    public int deleteFTmsorderbillsfeesById(Long fId) {
        return fTmsorderbillsfeesMapper.deleteFTmsorderbillsfeesById(fId);
    }
}
