package cn.wisenergy.service.impl;

import cn.wisenergy.common.utils.exception.BASE_RESP_CODE_ENUM;
import cn.wisenergy.common.utils.exception.BaseCustomException;
import cn.wisenergy.mapper.WorkProjectChangeMapper;
import cn.wisenergy.model.app.WorkProjectChange;
import cn.wisenergy.model.enums.ChangeTypeEnum;
import cn.wisenergy.service.WorkProjectChangeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenqi
 * @since 2021-01-19
 */
@Service
@Slf4j
public class WorkProjectChangeServiceImpl implements WorkProjectChangeService {

    @Autowired
    private WorkProjectChangeMapper workProjectChangeMapper;

    /**
     * 批量插入
     *
     * @param workProjectChanges 集合
     * @return 布尔值
     */
    @Override
    public Boolean insertbatch(List<WorkProjectChange> workProjectChanges) {
        log.info("WorkProjectChangeServiceImpl[]insertbatch[]input.param.List<workProjectChanges>:{}"+workProjectChanges);
        if (CollectionUtils.isEmpty(workProjectChanges)) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        int i = workProjectChangeMapper.insertBatch(workProjectChanges);
        return i!=0;
    }

    /**
     * 根据项目id查看更变记录
     *
     * @param projectId 项目id
     * @return 集合
     */
    @Override
    public List<WorkProjectChange> getByProjectId(Integer projectId) {
        log.info("WorkProjectChangeServiceImpl[]getByProjectId[]input.param。projectId:{}"+projectId);
        if (null == projectId) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        List<WorkProjectChange> workProjectChanges = workProjectChangeMapper.getByProjectId(projectId);
        if (CollectionUtils.isEmpty(workProjectChanges)) {
            return null;
        }
        // 给ChangeTypeName赋值
        for (WorkProjectChange workProjectChange : workProjectChanges) {
            for (ChangeTypeEnum value : ChangeTypeEnum.values()) {
                if (value.getChangeType().equals(workProjectChange.getChangeType())) {
                    workProjectChange.setChangeTypeName(value.getChangeTypeName());
                }
            }
        }
        return workProjectChanges;
    }
}
