package com.fdkankan.platform.agent.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.fdkankan.common.constant.TbStatus;
import com.fdkankan.platform.agent.entity.AgentAudit;
import com.fdkankan.platform.agent.mapper.IAgentAuditMapper;
import com.fdkankan.platform.agent.request.RequestAgentAudit;
import com.fdkankan.platform.agent.request.RequestNote;
import com.fdkankan.platform.agent.request.RequestSearch;
import com.fdkankan.platform.agent.service.IAgentAuditService;
import com.fdkankan.platform.agent.vo.AgentAuditVo;
import com.fdkankan.common.constant.ConstantRegex;
import com.fdkankan.common.validation.ValidationUtils;
import com.fdkankan.common.constant.ErrorCode;
import com.fdkankan.common.constant.ServerCode;
import com.fdkankan.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 代理商申请表 服务实现类
 * </p>
 *
 * @author 
 * @since 2021-12-24
 */
@Slf4j
@Service
public class AgentAuditServiceImpl extends ServiceImpl<IAgentAuditMapper, AgentAudit> implements IAgentAuditService {

    @Override
    public String saveByParam(RequestAgentAudit param) {
        if(param == null || StringUtils.isEmpty(param.getName()) || StringUtils.isEmpty(param.getAddress()) ||
                StringUtils.isEmpty(param.getCountry()) || StringUtils.isEmpty(param.getRegion()) ||
                StringUtils.isEmpty(param.getSurName()) || StringUtils.isEmpty(param.getUserName()) ||
                StringUtils.isEmpty(param.getPost()) || StringUtils.isEmpty(param.getAreaCode()) ||
                StringUtils.isEmpty(param.getPhone()) || StringUtils.isEmpty(param.getEmail())){
            throw new BusinessException(ErrorCode.MISSING_REQUIRED_PARAMETERS);
        }
        if(!param.getEmail().matches(ConstantRegex.EMAIL_REGEX)){
            throw new BusinessException(ErrorCode.EMAIL_ERROR);
        }
        AgentAudit agentAudit = new AgentAudit();
        BeanUtils.copyProperties(param,agentAudit);
        agentAudit.setNoteType(1);
        agentAudit.setState(1);
        if(!this.save(agentAudit)){
            throw new BusinessException(ErrorCode.ERROR_MSG);
        }
        return ServerCode.SUCCESS.message();
    }

    @Override
    public Page<AgentAudit> getPageList(Integer pageNum, Integer pageSize) {
        QueryWrapper<AgentAudit> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AgentAudit::getTbStatus, TbStatus.VALID.code());
        queryWrapper.lambda().orderByDesc(AgentAudit::getCreateTime);
        Page<AgentAudit> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return  this.page(page,queryWrapper);
    }

    @Override
    public String updateStatus(Long id, String state) {
        AgentAudit audit = this.getById(id);
        Integer status = Integer.valueOf(state);
        if (audit == null || !ValidationUtils.validateState(status)) {
            throw new BusinessException(ErrorCode.MISSING_REQUIRED_PARAMETERS);
        }
        audit.setState(status);
        if(!this.updateById(audit)){
            throw new BusinessException(ErrorCode.ERROR_MSG);
        }
        return ServerCode.SUCCESS.message();
    }

    @Override
    public String updateNote(RequestNote param) {
        if(param.getId() == null || param.getNoteType() == null){
            throw new BusinessException(ErrorCode.MISSING_REQUIRED_PARAMETERS);
        }
        AgentAudit audit = this.getById(param.getId());
        if(audit == null){
            throw new BusinessException(ErrorCode.NOT_RECORD);
        }
        if (!ValidationUtils.validateInteger(param.getNoteType())){
            throw new BusinessException(ErrorCode.MISSING_REQUIRED_PARAMETERS);
        }
        audit.setNoteType(param.getNoteType());
        audit.setNoteContent(param.getNoteContent());
        this.updateById(audit);
        return ServerCode.SUCCESS.message();
    }

    @Override
    public Page<AgentAuditVo> searchLike(RequestSearch param) {
        QueryWrapper<AgentAudit> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AgentAudit::getTbStatus,TbStatus.VALID.code());
        if(StringUtils.isNotBlank(param.getStartDate())){
            queryWrapper.lambda().ge(AgentAudit::getCreateTime,param.getStartDate());
        }
        if(StringUtils.isNotBlank(param.getEndDate())){
            queryWrapper.lambda().le(AgentAudit::getCreateTime,param.getEndDate());
        }
        if(StringUtils.isNotBlank(param.getItemName())){
            queryWrapper.lambda().like(AgentAudit::getAuditName,param.getItemName());
            queryWrapper.lambda().like(AgentAudit::getAddress,param.getItemName());
            queryWrapper.lambda().like(AgentAudit::getNoteContent,param.getItemName());
        }
        queryWrapper.lambda().orderByDesc(AgentAudit::getCreateTime);
        Page<AgentAudit> page = new Page<>();
        page.setCurrent(param.getPageNum());
        page.setSize(param.getPageSize());
        Page<AgentAudit> auditPage = this.page(page, queryWrapper);

        List<AgentAuditVo> voList = auditPage.getRecords().parallelStream().map(agentAudit -> {
            AgentAuditVo agentAuditVo = new AgentAuditVo();
            BeanUtils.copyProperties(agentAudit, agentAuditVo);
            return agentAuditVo;
        }).collect(Collectors.toList());

        Page<AgentAuditVo> auditVoPage = new Page<>();
        auditVoPage.setCurrent(page.getCurrent());
        auditVoPage.setSize(page.getSize());
        auditVoPage.setRecords(voList);
        auditVoPage.setTotal(page.getTotal());
        return auditVoPage;
    }
}
