package com.ruoyi.orderPlan.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.approvalFlow.domain.AuditItems;
import com.ruoyi.approvalFlow.service.impl.AuditItemsServiceImpl;
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.model.LoginUser;
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.domain.SysConfig;
import com.ruoyi.system.mapper.SysConfigMapper;
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 org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.math.BigDecimal;
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;

    @Autowired
    private SysConfigMapper sysConfigMapper;
    @Autowired
    private AuditItemsServiceImpl auditItemsService;


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

    /**
     * 单条费用提交
     *
     * @param cars 主表信息
     * @param fees 费用
     * @return 结果
     */
    @Override
    public AjaxResult insertFTmsorderbillsfeesAdd(String cars, String fees) {
        Map<String, Object> map = new HashMap<>();
        Ftmsorderbillscars tmsorderbillscars = JSONArray.parseObject(cars, Ftmsorderbillscars.class);
        Long pId = tmsorderbillscars.getId();
        Long orgId = tmsorderbillscars.getOrgId();
        //  费用添加
        Ftmsorderbillsfees ftmsorderbillsfees = JSONArray.parseObject(fees, Ftmsorderbillsfees.class);
        ftmsorderbillsfees.setfPid(pId);
        ftmsorderbillsfees.setOrgId(orgId);
        ftmsorderbillsfees.setfBillstatus(6L);
        if (StringUtils.isNull(ftmsorderbillsfees.getfId())) {
            ftmsorderbillsfees.setCreateTime(new Date());
            ftmsorderbillsfees.setCreateBy(SecurityUtils.getUsername());
            fTmsorderbillsfeesMapper.insertFTmsorderbillsfees(ftmsorderbillsfees);
        } else {
            ftmsorderbillsfees.setUpdateTime(new Date());
            ftmsorderbillsfees.setUpdateBy(SecurityUtils.getUsername());
            fTmsorderbillsfeesMapper.updateFTmsorderbillsfees(ftmsorderbillsfees);
        }
        map.put("ftmsorderbillsfeesList", fTmsorderbillsfeesMapper.selectFTmsorderbillsfeesById(ftmsorderbillsfees.getfId()));
        return AjaxResult.success(map);
    }

    /**
     * 单条费用暂存分表
     *
     * @param cars 主表信息
     * @param fees 费用
     * @return 结果
     * dongyongwei 2021-07-19
     */
    @Override
    public AjaxResult insertFTmsorderbillsfeesAddTemporaryStorage(String cars, String fees) {
        //car 存储到 car_modify
        Map<String, Object> map = new HashMap<>();
        Ftmsorderbillscars tmsorderbillscars = JSONArray.parseObject(cars, Ftmsorderbillscars.class);
        Long fId = tmsorderbillscars.getId();
        //先判断是否已经暂存过car_modify
        List<Ftmsorderbillscars> ftmsorderbillscarsList = ftmsorderbillscarsMapper.selectftmsorderbillscarsTemporaryStorageBySrcMId(fId);
        //如果已经暂存过,直接处理fees_modify
        Ftmsorderbillscars ftmsorderbillscarsTemporaryStorageAdded = new Ftmsorderbillscars();//暂存CAR
        if(ftmsorderbillscarsList.size()>0){
            ftmsorderbillscarsTemporaryStorageAdded = ftmsorderbillscarsList.get(0);
        }else{
            //如果没有暂存过,暂存并处理fees_modify
            Ftmsorderbillscars ftmsorderbillscarsTemporaryStorage = ftmsorderbillscarsMapper.selectftmsorderbillscarsById(fId);
            //处理car_modify规则数据
            ftmsorderbillscarsTemporaryStorage.setSrcMId(fId);
            int i=ftmsorderbillscarsMapper.insertftmsorderbillscarsTemporaryStorage(ftmsorderbillscarsTemporaryStorage);
            if(i>0){
                //先获取暂存car_modify数据出来
                if(ftmsorderbillscarsList.size()>0){
                    ftmsorderbillscarsTemporaryStorageAdded= ftmsorderbillscarsList.get(0);
                }
            }
        }
        //fees 存储到 fees_modify
        //  费用添加
        Ftmsorderbillsfees ftmsorderbillsfees = JSONArray.parseObject(fees, Ftmsorderbillsfees.class);
        ftmsorderbillsfees.setfPid(ftmsorderbillscarsTemporaryStorageAdded.getId());
        ftmsorderbillsfees.setOrgId(ftmsorderbillscarsTemporaryStorageAdded.getOrgId());
        //存储来源CAR的ID,方便回写
        ftmsorderbillsfees.setSrcMId(tmsorderbillscars.getId());
        ftmsorderbillsfees.setfBillstatus(0L);
        if (StringUtils.isNull(ftmsorderbillsfees.getfId())) {
            ftmsorderbillsfees.setCreateTime(new Date());
            ftmsorderbillsfees.setCreateBy(SecurityUtils.getUsername());
            fTmsorderbillsfeesMapper.insertFTmsorderbillsfeesTemporaryStorage(ftmsorderbillsfees);
        } else {
            ftmsorderbillsfees.setUpdateTime(new Date());
            ftmsorderbillsfees.setUpdateBy(SecurityUtils.getUsername());
            fTmsorderbillsfeesMapper.updateFTmsorderbillsfeesModify(ftmsorderbillsfees);
        }

        return AjaxResult.success();
    }

    /** 多条 */
    @Override
    public AjaxResult insertFTmsorderbillsfeesAddTemporaryStorageList(String cars, String fees) {
        List<Ftmsorderbillsfees> ftmsorderbillscarsList = JSONArray.parseArray(fees,Ftmsorderbillsfees.class);
        for (int i = 0; i < ftmsorderbillscarsList.size(); i++) {
            this.insertFTmsorderbillsfeesAddTemporaryStorage(cars, JSON.toJSONString(ftmsorderbillscarsList.get(i)));
        }
        return AjaxResult.success();
    }

    /**
     * 查询车队费用列表
     *
     * @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);
    }

    @Override
    public int deleteFTmsorderbillsfeesModifyById(Long fId) {
        return fTmsorderbillsfeesMapper.deleteFTmsorderbillsfeesModifyById(fId);
    }

    /**
     * 查询费用是否允许变更、删除
     *
     * @param fId 费用信息id
     * @return 结果
     */
    @Transactional
    @Override
    public AjaxResult ftmsorderbillsChange(Long fId, String types) {
        Ftmsorderbillsfees ftmsorderbillsfees = fTmsorderbillsfeesMapper.selectFTmsorderbillsfeesById(fId);
        if (StringUtils.isNull(ftmsorderbillsfees)) {
            if ("change".equals(types) || "revoke".equals(types)) {
                return AjaxResult.error("未找到该费用信息，请确认该费用信息是否存在");
            } else {
                fTmsorderbillsfeesMapper.deleteFTmsorderbillsfeesById(fId);
                return AjaxResult.success();
            }
        }
        if (StringUtils.isNull(ftmsorderbillsfees.getfBillstatus())) {
            if ("revoke".equals(types)) {
                return AjaxResult.error("费用状态异常，请确认该费用信息状态");
            } else if ("change".equals(types)) {
                return AjaxResult.success();
            }
        }
        if (ftmsorderbillsfees.getfBillstatus() == 3L) {
            if ("revoke".equals(types)) {
                return AjaxResult.error("该费用已被撤销，请勿重复撤销");
            } else if ("change".equals(types)) {
                return AjaxResult.error("该费用信息已撤销不允许修改");
            } else {
                fTmsorderbillsfeesMapper.deleteFTmsorderbillsfeesById(fId);
                return AjaxResult.success();
            }
        }
        if (ftmsorderbillsfees.getfBillstatus() == 6L) {
            if (StringUtils.isNotNull(ftmsorderbillsfees.getfAccamount()) && ftmsorderbillsfees.getfAccamount().compareTo(new BigDecimal(0)) == 1) {
                return AjaxResult.error("该费用信息已对账不允许修改");
            }
            if (StringUtils.isNotNull(ftmsorderbillsfees.getfStlamount()) && ftmsorderbillsfees.getfStlamount().compareTo(new BigDecimal(0)) == 1) {
                return AjaxResult.error("该费用信息已产生金额不允许修改");
            }
        }
        if ("delete".equals(types)) {
            fTmsorderbillsfeesMapper.deleteFTmsorderbillsfeesById(fId);
        } else if ("revoke".equals(types)) {
            ftmsorderbillsfees.setfBillstatus(3L);
            fTmsorderbillsfeesMapper.updateFTmsorderbillsfees(ftmsorderbillsfees);
        }
        return AjaxResult.success();
    }

    /**
     * 提交审批 暂存费用明细
     *
     * @param fId
     */
    @Override
    public AjaxResult submitOrderbillsfeesModifyApproval(Long fId,LoginUser loginUser) {
        //暂时直接回写数据
        //先获取审批数据行
        Ftmsorderbillsfees ftmsorderbillsfees = fTmsorderbillsfeesMapper.selectFTmsorderbillsfeesModifyById(fId);
        Long srcMId = ftmsorderbillsfees.getSrcMId();
        ftmsorderbillsfees.setfBillstatus(6L);
        fTmsorderbillsfeesMapper.updateFTmsorderbillsfeesModify(ftmsorderbillsfees);
        //回写逻辑,srcMId返回给pId,进行绑定
        ftmsorderbillsfees.setfPid(srcMId);
        ftmsorderbillsfees.setfId(null);
        fTmsorderbillsfeesMapper.insertFTmsorderbillsfees(ftmsorderbillsfees);
        return AjaxResult.success();
    }
}
