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

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuchen.saas.integration.feign.api.util.PageConverterUtils;
import com.yuchen.saas.park.api.dto.repair.PmgBatRepairRecordDTO;
import com.yuchen.saas.park.api.dto.repair.PmgBatRepairRecordProcessDTO;
import com.yuchen.saas.park.api.dto.repair.PmgBatRepairRecordQueryDTO;
import com.yuchen.saas.park.api.entity.repair.PmgBatRepairRecord;
import com.yuchen.saas.park.api.vo.repair.PmgBatRepairRecordAppVO;
import com.yuchen.saas.park.api.vo.repair.PmgBatRepairRecordVO;
import com.yuchen.saas.park.service.mapper.PmgBatRepairRecordMapper;
import com.yuchen.saas.park.service.service.PmgBatRepairRecordService;
import lombok.extern.slf4j.Slf4j;
import org.nest.springwrap.core.log.exception.ServiceException;
import org.nest.springwrap.core.mp.base.BaseServiceImpl;
import org.nest.springwrap.core.mp.support.NestPage;
import org.nest.springwrap.core.secure.ContextUser;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author 蔡文强
 * @Date 2023/4/13 15:56
 * @Description 报修记录表 服务实现类
 */
@Service
public class PmgBatRepairRecordServiceImpl extends BaseServiceImpl<PmgBatRepairRecordMapper, PmgBatRepairRecord> implements PmgBatRepairRecordService {

    private static final Logger logger = LoggerFactory.getLogger(PmgBatRepairRecordServiceImpl.class);

    @Override
    public Boolean saveRepairRecord(PmgBatRepairRecordDTO repairRecordDTO) {
        PmgBatRepairRecord repairRecord = new PmgBatRepairRecord();
        BeanUtils.copyProperties(repairRecordDTO, repairRecord);
        //平台级用户ID
        ContextUser contextUser = AuthUtil.getUser();
        logger.info("contextUser: {}", contextUser);
        if (Func.isEmpty(contextUser)) {
            throw new ServiceException("用户不存在!");
        }
        repairRecord.setUserDetailId(contextUser.getUserDetailId());
        repairRecord.setUserName(contextUser.getUserName());
        repairRecord.setPhone(contextUser.getDetail().getStr("phone"));
        repairRecord.setRepairTime(new Date());
        repairRecord.setRepairStatus(1);
        return save(repairRecord);
    }

    @Override
    public Boolean updateRepairRecord(PmgBatRepairRecordProcessDTO processDTO) {
        PmgBatRepairRecord repairRecord = getById(processDTO.getId());
        if (Func.isEmpty(repairRecord)) {
            throw new ServiceException("报修记录不存在!");
        }
        //平台级用户ID
        ContextUser contextUser = AuthUtil.getUser();
        logger.info("contextUser: {}", contextUser);
        if (Func.isEmpty(contextUser)) {
            throw new ServiceException("处理人用户不存在!");
        }
        repairRecord.setRepairStatus(2);
        repairRecord.setProcessUserDetailId(contextUser.getUserDetailId());
        repairRecord.setInstruction(processDTO.getInstruction());
        repairRecord.setProcessTime(new Date());
        return updateById(repairRecord);
    }

    @Override
    public PmgBatRepairRecordVO getRepairRecordDetailById(Long id) {
        PmgBatRepairRecord repairRecord = getById(id);
        PmgBatRepairRecordVO repairRecordVO = new PmgBatRepairRecordVO();
        BeanUtils.copyProperties(repairRecord, repairRecordVO);
        return repairRecordVO;
    }

    @Override
    public List<PmgBatRepairRecordAppVO> getMyRepairRecordList() {
        //平台级用户ID
        ContextUser contextUser = AuthUtil.getUser();
        logger.info("contextUser: {}", contextUser);
        if (Func.isEmpty(contextUser)) {
            throw new ServiceException("用户不存在!");
        }
        List<PmgBatRepairRecord> pmgBatRepairRecordList = lambdaQuery().eq(PmgBatRepairRecord::getUserDetailId, contextUser.getUserDetailId()).orderByDesc(PmgBatRepairRecord::getCreateTime).list();

        List<PmgBatRepairRecordAppVO> pmgBatRepairRecordAppVOList = BeanUtil.copy(pmgBatRepairRecordList, PmgBatRepairRecordAppVO.class);
        return pmgBatRepairRecordAppVOList;
    }


    @Override
    public NestPage<PmgBatRepairRecordVO> getRepairRecordPageList(PmgBatRepairRecordQueryDTO queryDTO) {
        Page<PmgBatRepairRecordVO> page = new Page<>(queryDTO.getCurrent(), queryDTO.getSize());
        page = baseMapper.getRepairRecordPageList(page, queryDTO);

        return PageConverterUtils.convert(page);
    }

}
