package com.ym.mec.web.controller.education;

import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.CooperationOrganDao;
import com.ym.mec.biz.dal.dao.OrganizationDao;
import com.ym.mec.biz.dal.entity.CooperationOrgan;
import com.ym.mec.biz.dal.entity.FinancialExpenditure;
import com.ym.mec.biz.dal.entity.Organization;
import com.ym.mec.biz.dal.entity.StudentVisit;
import com.ym.mec.biz.dal.enums.ExpenditureTypeEnum;
import com.ym.mec.biz.dal.enums.FeeProjectEnum;
import com.ym.mec.biz.service.FinancialExpenditureService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Api(tags = "支出记录（教务端）")
@RequestMapping("eduFinancialExpenditure")
@RestController
public class EduFinancialExpenditureController extends BaseController {
    @Autowired
    private FinancialExpenditureService financialExpenditureService;
    @Autowired
    private OrganizationDao organizationDao;
    @Autowired
    private CooperationOrganDao cooperationOrganDao;

    @ApiOperation(value = "批量添加支出记录")
    @PostMapping(value = "/batchAdd")
    public HttpResponseResult<List<FinancialExpenditure>> batchAdd(@RequestBody List<FinancialExpenditure> financialExpenditures) {
        List<Organization> organs = organizationDao.findAllOrgans();
        Map<String, Integer> organMap = organs.stream().collect(Collectors.toMap(Organization::getName, Organization::getId));
        List<CooperationOrgan> cooperationOrgans = cooperationOrganDao.getAllCooperationOrgan();
        Map<String, Integer> cooperationOrganMap = cooperationOrgans.stream().collect(Collectors.toMap(e -> e.getName() + e.getOrganId(), e -> e.getId()));
        for (FinancialExpenditure financialExpenditure : financialExpenditures) {
            if (organMap.containsKey(financialExpenditure.getOrganName())) {
                financialExpenditure.setOrganId(organMap.get(financialExpenditure.getOrganName()));
            }
            if (cooperationOrganMap.containsKey(financialExpenditure.getCooperationOrganName() + financialExpenditure.getOrganId())) {
                Integer cooperationOrganId = cooperationOrganMap.get(financialExpenditure.getCooperationOrganName() + financialExpenditure.getOrganId());
                financialExpenditure.setCooperationOrganId(cooperationOrganId);
            }
            for (ExpenditureTypeEnum typeEnum : ExpenditureTypeEnum.values()) {
                if (typeEnum.getDesc().equals(financialExpenditure.getFeeType())) {
                    financialExpenditure.setType(typeEnum);
                }
            }
            for (FeeProjectEnum feeProjectEnum : FeeProjectEnum.values()) {
                if (feeProjectEnum.getDesc().equals(financialExpenditure.getFeeProjectItem())) {
                    financialExpenditure.setFeeProject(feeProjectEnum);
                }
            }
        }
        return succeed(financialExpenditureService.batchAdd(financialExpenditures));
    }
}
