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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wn.common.datamodel.ModelConvertUtil;
import com.wn.common.validator.Validator;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.modules.app.common.dto.ContractDTO;
import org.springblade.modules.app.common.query.ContractQuery;
import org.springblade.modules.app.constant.DbField;
import org.springblade.modules.app.constant.ErrorCode;
import org.springblade.modules.app.entity.ContractDO;
import org.springblade.modules.app.mapper.ContractMapper;
import org.springblade.modules.app.service.IContractService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户合同信息表 服务实现类
 * </p>
 *
 * @author fuxinsheng
 * @since 2021-11-05
 */
@Service
public class ContractServiceImpl extends ServiceImpl<ContractMapper, ContractDO> implements IContractService {

    @Override
    public Long addContract(ContractDTO updateContractDTO) {
        ContractDO contractDO = updateContractDTO.copyTo(ContractDO.class);
        Validator.isTrue(save(contractDO), ErrorCode.SAVE_DATA_INTO_DB_ERROR);
        return contractDO.getId();
    }

    @Override
    public ContractDTO getContract(ContractQuery contractQuery) {
        QueryWrapper<ContractDO> wrapper = new QueryWrapper<>();
        wrapper.eq(contractQuery.getBuyerId() != null, DbField.BUYER_USER_ID, contractQuery.getBuyerId());
        wrapper.eq(contractQuery.getSalerId() != null, DbField.SALER_USER_ID, contractQuery.getSalerId());
        wrapper.eq(contractQuery.getState() != null, DbField.STATE, contractQuery.getState());
        wrapper.orderByDesc(DbField.CREATED_TIME);
        wrapper.last("limit 1");
        ContractDO contractDO = getOne(wrapper);
        return contractDO == null ? null : contractDO.copyTo(ContractDTO.class);
    }

    @Override
    public Boolean updateContract(ContractDTO contractDTO) {
        ContractDO contractDO = contractDTO.copyTo(ContractDO.class);
        Validator.isTrue(updateById(contractDO), ErrorCode.SAVE_DATA_INTO_DB_ERROR);
        return true;
    }

    @Override
    public ContractDTO getContractById(Long id) {
        Validator.notNull(id, ErrorCode.INPUT_PARAMETERS_NULL);
        ContractDO contractDO = getById(id);
        return contractDO == null ? null : contractDO.copyTo(ContractDTO.class);
    }

    @Override
    public Page<ContractDTO> pageContract(ContractQuery contractQuery) {
        Page<ContractDO> covertToPage = contractQuery.covertToPage();
        QueryWrapper<ContractDO> wrapper = new QueryWrapper<>();
        if(!StringUtil.isEmpty(contractQuery.getContractId())){
            wrapper.eq(DbField.ID, contractQuery.getContractId());
        }
        if(!StringUtil.isEmpty(contractQuery.getBuyerName())){
            wrapper.like(DbField.BUYER_USER_NAME, contractQuery.getBuyerName());
        }
        if(!StringUtil.isEmpty(contractQuery.getSalerName())){
            wrapper.like(DbField.SALER_USER_NAME, contractQuery.getSalerName());
        }
        if(!StringUtil.isEmpty(contractQuery.getStartTime())){
            wrapper.ge(DbField.CREATED_TIME, contractQuery.getStartTime());
        }
        if(!StringUtil.isEmpty(contractQuery.getEndTime())){
            wrapper.le(DbField.CREATED_TIME, contractQuery.getEndTime());
        }
        if(!StringUtil.isEmpty(contractQuery.getState())){
            wrapper.eq(DbField.STATE, contractQuery.getState());
        }

        wrapper.orderByDesc(DbField.CREATED_TIME);

        Page<ContractDO> page = page(covertToPage, wrapper);
        return ModelConvertUtil.copyPageTo(page, ContractDTO.class);
    }

    @Override
    public List<ContractDTO> getContractList(ContractQuery query) {
        Validator.notNull(query, ErrorCode.INPUT_PARAMETERS_NULL);
        QueryWrapper<ContractDO> wrapper = new QueryWrapper<>();
        wrapper.eq(query.getBuyerId() != null, DbField.BUYER_USER_ID, query.getBuyerId());
        wrapper.eq(query.getSalerId() != null, DbField.SALER_USER_ID, query.getSalerId());
        wrapper.likeRight(StringUtils.isNotEmpty(query.getContractName()), DbField.CONTRACT_NAME, query.getContractName());
        wrapper.eq(query.getState() != null, DbField.STATE, query.getState());
        wrapper.orderByDesc(DbField.CREATED_TIME);
        List<ContractDO> list = list(wrapper);
        return ModelConvertUtil.copyListTo(list, ContractDTO.class);
    }

}
