package org.springblade.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wn.common.datamodel.ModelConvertUtil;
import com.wn.common.validator.Validator;
import org.springblade.modules.app.common.dto.ApplicationInfoDTO;
import org.springblade.modules.app.common.query.ApplicationQuery;
import org.springblade.modules.app.constant.DbField;
import org.springblade.modules.app.constant.ErrorCode;
import org.springblade.modules.app.entity.ApplicationInfoDO;
import org.springblade.modules.app.mapper.ApplicationInfoMapper;
import org.springblade.modules.app.mapper.WaybillMapper;
import org.springblade.modules.app.service.IApplicationInfoService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 审批信息表 服务实现类
 * </p>
 *
 * @author fuxinsheng
 * @since 2021-11-05
 */
@Service
public class ApplicationInfoServiceImpl extends ServiceImpl<ApplicationInfoMapper, ApplicationInfoDO> implements IApplicationInfoService {

    @Autowired
    private ApplicationInfoMapper applicationInfoMapper;

    @Override
    public Long addApplicationInfo(ApplicationInfoDTO applicationInfoDTO) {
        ApplicationInfoDO applicationInfoDO = applicationInfoDTO.copyTo(ApplicationInfoDO.class);
        Validator.isTrue(save(applicationInfoDO), ErrorCode.SAVE_DATA_INTO_DB_ERROR);
        return applicationInfoDO.getId();
    }

    @Override
    public Boolean updateApplicationInfo(ApplicationInfoDTO applicationInfoDTO) {
//        Validator.notNull(applicationInfoDTO, ErrorCode.INPUT_PARAMETERS_NULL);
//        Validator.notNull(applicationInfoDTO.getId(), ErrorCode.INPUT_PARAMETERS_NULL);
        ApplicationInfoDO applicationInfoDO = getById(applicationInfoDTO.getId());
        if (applicationInfoDO != null) {
            updateById(applicationInfoDTO.copyTo(ApplicationInfoDO.class));
        }
        return true;
    }

    @Override
    public Boolean updateBatchApplicationInfo(List<ApplicationInfoDTO> applicationInfoDTOList) {
        Validator.isTrue(CollectionUtils.isNotEmpty(applicationInfoDTOList), ErrorCode.INPUT_PARAMETERS_NULL);
        List<ApplicationInfoDO> applicationInfoDOList = ModelConvertUtil.copyListTo(applicationInfoDTOList, ApplicationInfoDO.class);
        updateBatchById(applicationInfoDOList);
        return true;
    }

    @Override
    public List<ApplicationInfoDTO> listApplicationInfo(ApplicationQuery query) {
        QueryWrapper<ApplicationInfoDO> wrapper = initQuery(query);
        List<ApplicationInfoDO> list = list(wrapper);
        return CollectionUtils.isNotEmpty(list) ? ModelConvertUtil.copyListTo(list, ApplicationInfoDTO.class) : null;
    }

    @Override
    public ApplicationInfoDTO getApplicationInfoByQuery(ApplicationQuery query) {
        QueryWrapper<ApplicationInfoDO> wrapper = initQuery(query);
        wrapper.orderByDesc(DbField.CREATED_TIME);
        wrapper.last("limit 1");
        ApplicationInfoDO applicationInfoDO = getOne(wrapper);
        return applicationInfoDO == null ? null : applicationInfoDO.copyTo(ApplicationInfoDTO.class);
    }

    public QueryWrapper<ApplicationInfoDO> initQuery(ApplicationQuery query) {
        QueryWrapper<ApplicationInfoDO> wrapper = new QueryWrapper<>();
        wrapper.eq(query.getApplyUserId() != null, DbField.APPLY_USER_ID, query.getApplyUserId());
        wrapper.eq(query.getSubmitUserId() != null, DbField.SUBMIT_USER_ID, query.getSubmitUserId());
        wrapper.eq(query.getType() != null, DbField.TYPE, query.getType());
        wrapper.eq(query.getLinkId() != null, DbField.LINK_ID, query.getLinkId());
        wrapper.eq(query.getState() != null, DbField.STATE, query.getState());
        wrapper.eq(query.getVersion() != null, "version", query.getVersion());
        return wrapper;
    }

    @Override
    public Boolean realDeleteById(Long id){
        int count = applicationInfoMapper.realDeleteById(id);
        Validator.isTrue(count == 1, ErrorCode.DELETE_DATA_ERROR);
        return true;
    }

}
