package com.ankaibei.workFlow.webadmin.ankaibei.service.impl;

import com.alibaba.fastjson.JSON;
import com.ankaibei.workFlow.webadmin.ankaibei.entity.*;
import com.ankaibei.workFlow.webadmin.ankaibei.enums.CodeEnum;
import com.ankaibei.workFlow.webadmin.ankaibei.enums.CostEnum;
import com.ankaibei.workFlow.webadmin.ankaibei.mapper.*;
import com.ankaibei.workFlow.webadmin.ankaibei.service.CostInfoService;
import com.ankaibei.workFlow.webadmin.ankaibei.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author lwq
 * @version 1.0
 * @description:
 * @date 2022/9/26 14:25
 */
@Service
public class CostInfoServiceImpl implements CostInfoService {

    @Resource
    private CostInfoMapper costInfoMapper;
    @Resource
    private ProjectInfoMapper projectInfoMapper;
    @Resource
    private CommunicationMapper communicationMapper;
    @Resource
    private PurchaseInfoMapper purchaseInfoMapper;
    @Resource
    private RestInfoMapper restInfoMapper;
    @Resource
    private TrafficExpenseMapper trafficExpenseMapper;
    @Resource
    private RepastExpenseMapper repastExpenseMapper;
    @Resource
    private TravelExpenseMapper travelExpenseMapper;
    @Resource
    private ProjectAuditInfoMapper projectAuditInfoMapper;

    @Override
    public ResMsg addCostInfo(CostInfoEntity costInfoEntity) {
        costInfoEntity.setCostStatus("1");
        costInfoEntity.setPaymentDate(costInfoEntity.getPaymentDate() == null ? new Date() : costInfoEntity.getPaymentDate());
        costInfoEntity.setCreateDate(new Date());
        costInfoMapper.save(costInfoEntity);
        return new ResMsg(CodeEnum.OK);
    }

    @Override
    public ResMsg findCostInfoAll(CostInfoEntity costInfoEntity, Integer pageIndex, Integer pageSize) {
        Pageable page = PageRequest.of(pageIndex - 1, pageSize);
        String paymentDate = null;
        if (costInfoEntity.getPaymentDate() != null) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            paymentDate = dateFormat.format(costInfoEntity.getPaymentDate()) + " 23:59:59";
        }
        Page<Map<String, String>> costInfoAll = costInfoMapper.findCostInfoAll(costInfoEntity.getFlowTaskStatus(), costInfoEntity.getHappenDate(), paymentDate, page);
        return new ResMsg(CodeEnum.OK, ResMsg.getMap(costInfoAll.map(map -> {
            Map<Object, Object> map1 = new HashMap<>();
            map1.putAll(map);
            String projectId = map.get("projectName");
            Optional<ProjectInfoEntity> byId = projectInfoMapper.findById(Integer.valueOf(projectId));
            map1.put("projectName", byId.orElse(null) != null ? byId.orElse(null).getProjectName() : null);
            return map1;
        }).getContent(), costInfoAll.getTotalElements()));
    }

    @Override
    public boolean deleteCostInfo(List<CostInfoEntity> costInfoEntities) {
        for (CostInfoEntity info : costInfoEntities) {
            costInfoMapper.deleteById(info.getCostId());
        }
        return true;
    }

    @Override
    public ResMsg updateCostInfo(CostInfoEntity costInfoEntity) {
        Optional<CostInfoEntity> byId = costInfoMapper.findById(costInfoEntity.getCostId());
        CostInfoEntity cost = byId.orElse(null);
        if (StringUtils.isEmpty(cost)) {
            return new ResMsg(CodeEnum.PARAM_IS_NULL);
        }
        costInfoEntity.setCostStatus(cost.getCostStatus() == null ? "1" : cost.getCostStatus());
        costInfoEntity.setProposer(cost.getProposer() == null ? "1" : cost.getProposer());
        costInfoMapper.save(costInfoEntity);
        return new ResMsg(CodeEnum.OK);
    }

    @Override
    public ResMsg findCostInfoDetails(CostInfoEntity costInfoEntity) {
        Optional<CostInfoEntity> byId = costInfoMapper.findById(costInfoEntity.getCostId());
        CostInfoEntity costInfo = byId.orElse(null);
        switch (CostEnum.getEnumByCode(costInfoEntity.getCostType())) {
            case COMMUNICATION:
                CommunicationInfoEntity communicationInfoEntity = communicationMapper.findById(costInfo.getApplyCostId()).orElse(null);
                ProjectInfoEntity projectInfoEntity = projectInfoMapper.findById(Integer.valueOf(communicationInfoEntity.getProjectName())).orElse(null);
                communicationInfoEntity.setProjectName(projectInfoEntity.getProjectName());
                return new ResMsg(CodeEnum.OK, communicationInfoEntity);
            case PURCHASE:
                PurchaseInfoEntity purchaseInfoEntity = purchaseInfoMapper.findById(costInfo.getApplyCostId()).orElse(null);
                String purchaseMessage = purchaseInfoEntity.getPurchaseMessage();
                PurchaseInfoVo purchaseInfoVo = new PurchaseInfoVo();
                BeanUtils.copyProperties(purchaseInfoEntity, purchaseInfoVo);
                List<PurchaseInfoVo.PurChaseMessage> purChaseMessages = JSON.parseArray(purchaseMessage, PurchaseInfoVo.PurChaseMessage.class);
                ProjectInfoEntity projectInfoEntity1 = projectInfoMapper.findById(Integer.valueOf(purchaseInfoEntity.getProjectName())).orElse(null);
                purchaseInfoVo.setProjectName(projectInfoEntity1.getProjectName());
                purchaseInfoVo.setPurchaseMessage(purChaseMessages);
                return new ResMsg(CodeEnum.OK, purchaseInfoVo);
            case REST:
                RestsInfoEntity restsInfoEntity = restInfoMapper.findById(costInfo.getApplyCostId()).orElse(null);
                String restsMessage = restsInfoEntity.getRestsMessage();
                RestsInfoVo restsInfoVo = new RestsInfoVo();
                BeanUtils.copyProperties(restsInfoEntity, restsInfoVo);
                List<RestsInfoVo.RestsMessage> restsMessages = JSON.parseArray(restsMessage, RestsInfoVo.RestsMessage.class);
                ProjectInfoEntity projectInfoEntity2 = projectInfoMapper.findById(Integer.valueOf(restsInfoEntity.getProjectName())).orElse(null);
                restsInfoVo.setProjectName(projectInfoEntity2.getProjectName());
                restsInfoVo.setRestsMessage(restsMessages);
                return new ResMsg(CodeEnum.OK, restsInfoVo);
            case TRANSPORTATION:
                TrafficExpenseEntity trafficExpenseEntity = trafficExpenseMapper.findById(costInfo.getApplyCostId()).orElse(null);
                String subject = trafficExpenseEntity.getSubject();
                TrafficExpenseVo trafficExpenseVo = new TrafficExpenseVo();
                BeanUtils.copyProperties(trafficExpenseEntity, trafficExpenseVo);
                List<TrafficExpenseVo.TrafficExpenseSubject> trafficExpenseSubjects = JSON.parseArray(subject, TrafficExpenseVo.TrafficExpenseSubject.class);
                ProjectInfoEntity projectInfoEntity3 = projectInfoMapper.findById(Integer.valueOf(trafficExpenseEntity.getProjectName())).orElse(null);
                trafficExpenseVo.setProjectName(projectInfoEntity3.getProjectName());
                trafficExpenseVo.setSubject(trafficExpenseSubjects);
                return new ResMsg(CodeEnum.OK, trafficExpenseVo);
            case REPAST:
                RepastExpenseEntity repastExpenseEntity = repastExpenseMapper.findById(costInfo.getApplyCostId()).orElse(null);
                String subject1 = repastExpenseEntity.getSubject();
                RepastExpenseVo repastExpenseVo = new RepastExpenseVo();
                BeanUtils.copyProperties(repastExpenseEntity, repastExpenseVo);
                List<RepastExpenseVo.RepastExpenseSubject> repastExpenseSubjects = JSON.parseArray(subject1, RepastExpenseVo.RepastExpenseSubject.class);
                ProjectInfoEntity projectInfoEntity4 = projectInfoMapper.findById(Integer.valueOf(repastExpenseEntity.getProjectName())).orElse(null);
                repastExpenseVo.setProjectName(projectInfoEntity4.getProjectName());
                repastExpenseVo.setSubject(repastExpenseSubjects);
                return new ResMsg(CodeEnum.OK, repastExpenseVo);
            case TRAVEL:
                TravelExpenseEntity travelExpenseEntity = travelExpenseMapper.findById(costInfo.getApplyCostId()).orElse(null);
                String subject2 = travelExpenseEntity.getSubject();
                TravelExpenseVo travelExpenseVo = new TravelExpenseVo();
                BeanUtils.copyProperties(travelExpenseEntity, travelExpenseVo);
                List<TravelExpenseVo.TravelExpenseSubject> travelExpenseSubjects = JSON.parseArray(subject2, TravelExpenseVo.TravelExpenseSubject.class);
                ProjectInfoEntity projectInfoEntity5 = projectInfoMapper.findById(Integer.valueOf(travelExpenseEntity.getProjectName())).orElse(null);
                travelExpenseVo.setProjectName(projectInfoEntity5.getProjectName());
                travelExpenseVo.setSubject(travelExpenseSubjects);
                return new ResMsg(CodeEnum.OK, travelExpenseVo);
            default:
                break;
        }
        return new ResMsg(CodeEnum.NO_DATA);
    }

    @Override
    public ResMsg findProjectAuditList(String id, UserInfoVo userInfoVo) {
        Pageable page = PageRequest.of(userInfoVo.getPageIndex() - 1, userInfoVo.getPageSize());
        Page<Map<String, String>> byPrintcipalEquals = projectAuditInfoMapper.findByPrintcipalEquals(id, page);
        return new ResMsg(CodeEnum.OK, ResMsg.getMap(byPrintcipalEquals.map(map -> {
            Map<Object, Object> map1 = new HashMap<>();
            map1.putAll(map);
            String projectId = map.get("projectName");
            Optional<ProjectInfoEntity> byId = projectInfoMapper.findById(Integer.valueOf(projectId));
            map1.put("projectName", byId.orElse(null) != null ? byId.orElse(null).getProjectName() : null);
            return map1;
        }).getContent(), byPrintcipalEquals.getTotalElements()));
    }
}
