package com.securitycontrol.background.service.impl;

import com.securitycontrol.background.mapper.ISignProMapper;
import com.securitycontrol.background.service.ISignProService;
import com.securitycontrol.common.core.utils.StringUtils;
import com.securitycontrol.common.core.utils.uuid.IdUtils;
import com.securitycontrol.common.core.web.domain.AjaxResult;
import com.securitycontrol.common.security.utils.ValidatorsUtils;
import com.securitycontrol.entity.background.dto.ParamDto;
import com.securitycontrol.entity.background.vo.HumanManageVo;
import com.securitycontrol.entity.background.vo.ProjectVo;
import com.securitycontrol.entity.background.vo.SignProVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author：cwchen
 * @date：2024-04-01-19:25
 * @version：1.0
 * @description：项目工程/单项工程-业务逻辑层
 */
@Service(value = "ISignProService")
@Slf4j
public class SignProServiceImpl implements ISignProService {

    @Resource(name = "ISignProMapper")
    private ISignProMapper mapper;

    @Resource(name = "ValidatorsUtils")
    private ValidatorsUtils validatorsUtils;

    @Override
    public List<ProjectVo> getProLists(ParamDto dto) {
        List<ProjectVo> list = new ArrayList<>();
        try {
            list = mapper.getProLists(dto);
        } catch (Exception e) {
            log.error("工程列表");
        }
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult addOrUpdateProject(ProjectVo vo) {
        try {
            String validResult = validatorsUtils.valid(vo, ProjectVo.Query.class);
            if (StringUtils.isNotBlank(validResult)) {
                return AjaxResult.error(validResult);
            }
            int result = mapper.isProject(vo);
            if (result > 0) {
                return AjaxResult.error("项目编码不能重复");
            }
            if (StringUtils.isEmpty(vo.getId())) {
                String id = IdUtils.getUUId();
                vo.setId(id);
                vo.setType(1);
            } else {
                vo.setType(2);
            }
            mapper.addOrUpdateProject(vo);
        } catch (Exception e) {
            log.error("新增/修改项目",e);
            //手动回滚异常
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult getProjectById(ParamDto dto) {
        ProjectVo vo = new ProjectVo();
        try {
            if(StringUtils.isEmpty(dto.getId())){
                return AjaxResult.error("参数不完整");
            }
            vo = mapper.getProjectById(dto);
        } catch (Exception e) {
            log.error("项目详情",e);
        }
        return AjaxResult.success(vo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult delProjectById(ParamDto dto) {
        try {
            if(StringUtils.isEmpty(dto.getId()) || StringUtils.isEmpty(dto.getProNo())){
                return AjaxResult.error("参数不完整");
            }
            int result = mapper.isHasChildPro(dto);
            if(result > 0){
                return AjaxResult.error("项目包含单项工程");
            }
            mapper.delProjectById(dto);
        } catch (Exception e) {
            log.error("删除项目",e);
            //手动回滚异常
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }

    @Override
    public List<SignProVo> getSignProLists(ParamDto dto) {
        List<SignProVo> list = new ArrayList<>();
        try {
            list = mapper.getSignProLists(dto);
        } catch (Exception e) {
            log.error("单项工程列表",e);
        }
        return list;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult addOrUpdateSignProject(SignProVo vo) {
        try {
            String validResult = validatorsUtils.valid(vo, SignProVo.Query.class);
            if (StringUtils.isNotBlank(validResult)) {
                return AjaxResult.error(validResult);
            }
            int result = mapper.isSignPro(vo);
            if (result > 0) {
                return AjaxResult.error("单项编码不能重复");
            }
            if (StringUtils.isEmpty(vo.getId())) {
                String id = IdUtils.getUUId();
                vo.setId(id);
                vo.setType(1);
            } else {
                vo.setType(2);
            }
            mapper.addOrUpdateSignProject(vo);
        } catch (Exception e) {
            log.error("新增/修改单项工程",e);
            //手动回滚异常
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult getSignProById(ParamDto dto) {
        SignProVo vo = new SignProVo();
        try {
            if(StringUtils.isEmpty(dto.getId())){
                return AjaxResult.error("参数不完整");
            }
            vo = mapper.getSignProById(dto);
        } catch (Exception e) {
            log.error("单项工程详情",e);
        }
        return AjaxResult.success(vo);
    }

    @Override
    public AjaxResult delSignProById(ParamDto dto) {
        try {
            if(StringUtils.isEmpty(dto.getId()) || StringUtils.isEmpty(dto.getSignNo())){
                return AjaxResult.error("参数不完整");
            }
            int result = mapper.isHasBidChildPro(dto);
            if(result > 0){
                return AjaxResult.error("单项工程包含标段工程");
            }
            mapper.delSignProById(dto);
        } catch (Exception e) {
            log.error("删除单项工程",e);
            //手动回滚异常
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }
}
