package com.yuchen.saas.park.service.service.repair.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuchen.saas.manage.system.api.feign.IOrgUserClient;
import com.yuchen.saas.park.api.dto.repair.*;
import com.yuchen.saas.park.api.entity.repair.SaasComplainSuggest;
import com.yuchen.saas.park.api.entity.repair.SaasMatterRepair;
import com.yuchen.saas.park.api.vo.repair.SaasComplainSuggestVo;
import com.yuchen.saas.park.service.mapper.repair.SaasComplainSuggestMapper;
import com.yuchen.saas.park.service.service.PmgBatParkInfoService;
import com.yuchen.saas.park.service.service.repair.ISaasComplainSuggestService;
import com.yuchen.saas.park.service.service.repair.ISaasMatterRepairTypeService;
import org.nest.springwrap.core.log.exception.ServiceException;
import org.nest.springwrap.core.mp.base.BaseServiceImpl;
import org.nest.springwrap.core.mp.support.Condition;
import org.nest.springwrap.core.mp.support.NestPage;
import org.nest.springwrap.core.secure.utils.AuthUtil;
import org.nest.springwrap.core.tool.utils.BeanUtil;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 投诉建议表 服务实现类
 * </p>
 *
 * @author caiwq
 * @since 2024-04-26
 */
@Service
public class SaasComplainSuggestServiceImpl extends BaseServiceImpl<SaasComplainSuggestMapper, SaasComplainSuggest> implements ISaasComplainSuggestService {

    @Resource
    private IOrgUserClient iOrgUserClient;
    @Resource
    private ISaasMatterRepairTypeService matterRepairTypeService;
    @Resource
    private PmgBatParkInfoService pmgBatParkInfoService;

    @Override
    public Boolean add(SaasComplainSuggestDto complainSuggestDto) {
        SaasComplainSuggest complainSuggest = BeanUtil.copyProperties(complainSuggestDto, SaasComplainSuggest.class);
        //客户ID
        Long customerId = AuthUtil.getUser().getCustomerManageId();
        complainSuggest.setCustomerId(customerId);
        //录单人
        Long recordUserId = AuthUtil.getUser().getOrgUserId();
        complainSuggest.setRecordUserId(recordUserId);
        return save(complainSuggest);
    }

    @Override
    public Boolean handle(SaasComplainSuggestHandleDto handleDto) {
        SaasComplainSuggest complainSuggest = getById(handleDto.getId());
        if (Func.isEmpty(complainSuggest)) {
            throw new ServiceException("投诉建议不存在");
        }
        complainSuggest.setComplainStatus(2);
        complainSuggest.setHandleUserId(AuthUtil.getUser().getOrgUserId());
        complainSuggest.setHandleTime(new Date());
        complainSuggest.setRemark(handleDto.getRemark());
        return updateById(complainSuggest);
    }

    @Override
    public Boolean remove(Long id) {
        return removeById(id);
    }

    @Override
    public Boolean cancle(SaasComplainSuggestCancleDto cancleDto) {
        SaasComplainSuggest complainSuggest = getById(cancleDto.getId());
        if (Func.isEmpty(complainSuggest)) {
            throw new ServiceException("投诉建议不存在");
        }
        complainSuggest.setComplainStatus(3);
        complainSuggest.setCancleDesc(cancleDto.getCancleDesc());
        return updateById(complainSuggest);
    }

    @Override
    public Boolean evaluate(SaasComplainSuggestEvaluateDto evaluateDto) {
        SaasComplainSuggest complainSuggest = getById(evaluateDto.getId());
        if (Func.isEmpty(complainSuggest)) {
            throw new ServiceException("投诉建议不存在");
        }
        complainSuggest.setEvaluateLevel(evaluateDto.getEvaluateLevel());
        complainSuggest.setEvaluateTag(evaluateDto.getEvaluateTag());
        complainSuggest.setEvaluateContent(evaluateDto.getEvaluateContent());
        return updateById(complainSuggest);
    }

    @Override
    public SaasComplainSuggestVo getDetail(Long id) {
        SaasComplainSuggestVo complainSuggestVo = BeanUtil.copyProperties(getById(id), SaasComplainSuggestVo.class);
        complainSuggestVo.setProjectName(pmgBatParkInfoService.getById(complainSuggestVo.getProjectId()).getParkName());
        complainSuggestVo.setComplainTypeName(matterRepairTypeService.getById(complainSuggestVo.getComplainTypeId()).getName());
        if(complainSuggestVo.getComplainStatus() == 2) {
            complainSuggestVo.setHandleUserName(iOrgUserClient.detail(complainSuggestVo.getHandleUserId()).getData().getName());
        }
        return complainSuggestVo;
    }

    @Override
    public NestPage<SaasComplainSuggestVo> list(SaasComplainSuggestQueryDto queryDto) {
        LambdaQueryWrapper<SaasComplainSuggest> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Func.isNotEmpty(queryDto.getProjectId()), SaasComplainSuggest::getProjectId, queryDto.getProjectId());
        queryWrapper.eq(Func.isNotEmpty(queryDto.getRecordUserId()), SaasComplainSuggest::getRecordUserId, queryDto.getRecordUserId());
        queryWrapper.eq(Func.isNotEmpty(queryDto.getComplainTypeId()), SaasComplainSuggest::getComplainTypeId, queryDto.getComplainTypeId());
        queryWrapper.like(Func.isNotEmpty(queryDto.getComplainContent()), SaasComplainSuggest::getComplainContent, queryDto.getComplainContent());
        queryWrapper.eq(Func.isNotEmpty(queryDto.getComplainStatus()), SaasComplainSuggest::getComplainStatus, queryDto.getComplainStatus());
        queryWrapper.like(Func.isNotEmpty(queryDto.getLinkMan()), SaasComplainSuggest::getLinkMan, queryDto.getLinkMan());
        queryWrapper.like(Func.isNotEmpty(queryDto.getLinkPhone()), SaasComplainSuggest::getLinkPhone, queryDto.getLinkPhone());
        queryWrapper.ge(Func.isNotEmpty(queryDto.getHandleStartTime()), SaasComplainSuggest::getHandleTime, queryDto.getHandleStartTime());
        queryWrapper.le(Func.isNotEmpty(queryDto.getHandleEndTime()), SaasComplainSuggest::getHandleTime, queryDto.getHandleEndTime());
        queryWrapper.ge(Func.isNotEmpty(queryDto.getSubmitStartTime()), SaasComplainSuggest::getCreateTime, queryDto.getSubmitStartTime());
        queryWrapper.le(Func.isNotEmpty(queryDto.getSubmitEndTime()), SaasComplainSuggest::getCreateTime, queryDto.getSubmitEndTime());
        queryWrapper.orderByDesc(SaasComplainSuggest::getCreateTime);
        IPage<SaasComplainSuggest> page = this.page(Condition.getPage(queryDto), queryWrapper);
        IPage<SaasComplainSuggestVo> result = new Page<>(queryDto.getCurrent(), queryDto.getSize());
        List<SaasComplainSuggest> recordList = page.getRecords();
        List<SaasComplainSuggestVo> list = BeanUtil.copyProperties(recordList, SaasComplainSuggestVo.class);
        for(SaasComplainSuggestVo vo: list) {
            vo.setProjectName(pmgBatParkInfoService.getById(vo.getProjectId()).getParkName());
            vo.setComplainTypeName(matterRepairTypeService.getById(vo.getComplainTypeId()).getName());
            if(vo.getComplainStatus() == 2) {
                vo.setHandleUserName(iOrgUserClient.detail(vo.getHandleUserId()).getData().getName());
            }
        }
        result.setRecords(list);
        result.setTotal(page.getTotal());
        return NestPage.of(result);
    }

}
