package com.ruoyi.orderPlan.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.orderPlan.domain.Ftmsorderbillscars;
import com.ruoyi.orderPlan.mapper.FtmsorderbillsMapper;
import com.ruoyi.orderPlan.mapper.FtmsorderbillscarsMapper;
import com.ruoyi.orderPlan.domain.Ftmsorderbillsfees;
import com.ruoyi.orderPlan.mapper.FtmsorderbillsfeesMapper;
import com.ruoyi.orderPlan.service.IftmsorderbillsfeesService;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 订单费用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 = ftmsorderbillscarsMapper.selectTmsorderbillsCarsByDriver(id);
        // 费用信息
        Ftmsorderbillsfees ftmsorderbillsfees = new Ftmsorderbillsfees();
        ftmsorderbillsfees.setfPid(id);
        map.put("ftmsorderbillsfees", fTmsorderbillsfeesMapper.selectFTmsorderbillsfeesList(ftmsorderbillsfees)); // 费用
        return map;
    }

    /**
     *  费用提交、保存
     *
     * @param cars      主表信息
     * @param status    状态
     * @param fees      费用
     * @return  结果
     */
    @Override
    public AjaxResult insertFTmsorderbillsfeesAdd(String cars, long status, String fees) {
        Map<String, Object> map = new HashMap<>();
        Ftmsorderbillscars tmsorderbillscars = JSONArray.parseObject(cars, Ftmsorderbillscars.class);
        Long pId = tmsorderbillscars.getId();
        Long orgId = tmsorderbillscars.getOrgId();
        //  费用添加
        if (StringUtils.isNotNull(fees) && !"[]".equals(fees)) {
            JSONArray jsonDrArray = JSONArray.parseArray(fees);
            List<Ftmsorderbillsfees> ftmsorderbillsfeesList = JSONObject.parseArray(jsonDrArray.toJSONString(), Ftmsorderbillsfees.class);
            fTmsorderbillsfeesMapper.deleteFTmsorderbillsfeesByPId(pId);
            for (Ftmsorderbillsfees ftmsorderbillsfees : ftmsorderbillsfeesList) {
                ftmsorderbillsfees.setfPid(pId);
                ftmsorderbillsfees.setOrgId(orgId);
                ftmsorderbillsfees.setfBillstatus(status);
                ftmsorderbillsfees.setCreateTime(new Date());
                ftmsorderbillsfees.setCreateBy(SecurityUtils.getUsername());
                fTmsorderbillsfeesMapper.insertFTmsorderbillsfees(ftmsorderbillsfees);
            }
        }
        Ftmsorderbillsfees ftmsorderbillsfees = new Ftmsorderbillsfees();
        ftmsorderbillsfees.setfPid(pId);
        map.put("ftmsorderbillsfeesList",fTmsorderbillsfeesMapper.selectFTmsorderbillsfeesList(ftmsorderbillsfees));
        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);
    }
}
