package com.bonus.sgzb.material.service.impl;

import com.bonus.sgzb.common.core.web.domain.AjaxResult;
import com.bonus.sgzb.material.config.ExceptionEnum;
import com.bonus.sgzb.material.domain.CheckDetailsInfo;
import com.bonus.sgzb.material.domain.PlanManagementDto;
import com.bonus.sgzb.material.domain.PlanManagementVO;
import com.bonus.sgzb.material.domain.PlanVO;
import com.bonus.sgzb.material.mapper.PlanManagementMapper;
import com.bonus.sgzb.material.service.PlanManagementService;
import lombok.extern.slf4j.Slf4j;
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.List;

/**
 * 计划管理服务实现层
 * @Author ma_sh
 * @create 2024/4/9 9:34
 */
@Service
@Slf4j
public class PlanManagementServiceImpl implements PlanManagementService {

    @Autowired
    private PlanManagementMapper planManagementMapper;

    /**
     * 获取计划管理列表
     * @param dto
     * @return
     */
    @Override
    public List<PlanVO> getList(PlanManagementDto dto) {
        return selectPlanInfo(dto);
    }

    /**
     * 查询计划管理列表
     * @param dto
     * @return
     */
    private List<PlanVO> selectPlanInfo(PlanManagementDto dto) {
        return planManagementMapper.selectPlanInfo(dto);
    }

    /**
     * 修改计划管理
     * @param dto
     * @return
     */
    @Override
    public Integer update(PlanManagementDto dto) {
        return null;
    }

    /**
     * 新增计划管理
     * @param dto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult insert(PlanManagementDto dto) {
        int res = 0;
        try {
            res = insertPlanManagement(dto);
            if (res == 0) {
                log.error("insertPlanManagement方法插入异常");
                throw new RuntimeException("insertPlanManagement方法插入异常");
            }
        } catch (Exception e) {
            log.error("新增计划管理异常:{}",e.getMessage());
            // 添加事务回滚逻辑，保证入库全部成功或者全部失败
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error(ExceptionEnum.SAVE_TO_DATABASE.getCode(), ExceptionEnum.SAVE_TO_DATABASE.getMsg());
        }
        return AjaxResult.success(res);
    }

    /**
     * 新增保存到计划管理表
     * @param dto
     * @return
     */
    private int insertPlanManagement(PlanManagementDto dto) {
        int res = 0;
        for (CheckDetailsInfo info : dto.getCheckDetailsList()) {
            info.setRemark(dto.getRemark());
            info.setCreateBy(dto.getCreateBy());
            info.setUpdateBy(dto.getUpdateBy());
            if (info.getPurchasePrice() != null && info.getPurchaseNum() != null) {
                BigDecimal total = info.getPurchasePrice().multiply(new BigDecimal(info.getPurchaseNum()));
//                total = (double) Math.round(total * 100) / 100;
                info.setTotalPrice(total);
            } else {
                info.setTotalPrice(new BigDecimal(0));
            }
            res += planManagementMapper.insertPlanNeedInfo(info);
            res += planManagementMapper.insertPlanNeedDetail(info);
        }
        return res;
    }

    /**
     * 根据id删除计划管理
     * @param planId
     * @return
     */
    @Override
    public Integer deleteById(Long planId) {
        return null;
    }

    /**
     * 新增计划借调管理
     * @param dto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult insertManage(PlanManagementDto dto) {
        int res = 0;
        try {
            res = insertManagePlan(dto);
            if (res == 0) {
                throw new RuntimeException("新增计划借调管理异常");
            }
        } catch (Exception e) {
            log.error("新增计划借调管理异常:{}",e.getMessage());
            // 添加事务回滚逻辑，保证入库全部成功或者全部失败
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error(ExceptionEnum.SAVE_TO_DATABASE.getCode(), ExceptionEnum.SAVE_TO_DATABASE.getMsg());
        }
        return AjaxResult.success(res);
    }

    /**
     * 获取计划借调管理列表
     * @param dto
     * @return
     */
    @Override
    public List<PlanManagementVO> getManageList(PlanManagementDto dto) {
        return planManagementMapper.selectBorrow(dto);
    }

    /**
     * 新增计划借调管理方法抽取
     * @param dto
     * @return
     */
    private int insertManagePlan(PlanManagementDto dto) {
        int res = 0;
        //新增plan_borrow_info
        res += planManagementMapper.insertPlanBorrow(dto);
        //新增plan_borrow_details
        for (CheckDetailsInfo checkDetailsInfo : dto.getCheckDetailsList()) {
            checkDetailsInfo.setPlanId(dto.getBorrowId());
        }
        res += planManagementMapper.insertPlanBorrowDetail(dto.getCheckDetailsList());
        return res;
    }

}
