package cn.iocoder.yudao.module.base.service.impl;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.base.entity.RepairRecords;
import cn.iocoder.yudao.module.base.entity.RepairRecordsItem;
import cn.iocoder.yudao.module.base.mapper.RepairRecordsMapper;
import cn.iocoder.yudao.module.base.service.RepairRecordsItemService;
import cn.iocoder.yudao.module.base.service.RepairRecordsService;
import cn.iocoder.yudao.module.base.vo.RepairRecordsPageReqVO;
import cn.iocoder.yudao.module.base.vo.RepairRecordsRespVO;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.common.BaseConstants.REPAIR_RECORD_TYPE_RECORD;

/**
 * 维修记录 Service 实现类
 *
 * @author lzt
 */
@Service
@Validated
public class RepairRecordsServiceImpl extends ServiceImpl<RepairRecordsMapper, RepairRecords> implements RepairRecordsService {

    @Resource
    private RepairRecordsMapper repairRecordsMapper;
    @Resource
    private AdminUserApi userApi;
    @Resource
    private RepairRecordsItemService itemService;


    /**
     * 保存维修记录
     *
     * @param ticketId     工单id
     * @param repairItemId 工单子表id
     * @param type         工作类型（数据字典：repair_records_type;后端对应 RecordTypeEnum 枚举）
     * @param remark       备注
     * @param images       图片(相对路径按照“，”分隔)
     * @author PQZ
     * @date 14:51 2024/10/11
     **/
    @Override
    public void saveRepairRecord(String ticketId, String repairItemId, String type, String remark, String images) {
        //获取当前登录用户
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        AdminUserRespDTO loginUser = userApi.getUser(userId);
        //初始化维修记录
        RepairRecords repairRecords = new RepairRecords();
        repairRecords.setTicketId(ticketId);
        repairRecords.setRepairItemId(repairItemId);
        repairRecords.setType(type);
        repairRecords.setRemark(remark);
        repairRecords.setDealUserId(loginUser.getId());
        repairRecords.setDealUserName(loginUser.getNickname());
        //保存维修记录
        save(repairRecords);
        //保存附件信息
        itemService.saveItem(repairRecords.getId(), ticketId, repairItemId, images);
    }

    /**
     * 根据条件查询维修记录
     *
     * @param pageReqVO RepairRecordsPageReqVO实体
     * @return java.util.List<cn.iocoder.yudao.module.base.vo.RepairRecordsRespVO>
     * @author PQZ
     * @date 15:14 2024/10/11
     **/
    @Override
    public List<RepairRecordsRespVO> queryList(RepairRecordsPageReqVO pageReqVO) {
        //根据条件查询维修记录
        List<RepairRecordsRespVO> list = repairRecordsMapper.queryRepairRecords(pageReqVO);
        //为每一条维修记录设置查询附件
        list.forEach(item -> {
            List<RepairRecordsItem> itemList = itemService.getByMainId(REPAIR_RECORD_TYPE_RECORD, item.getId(), pageReqVO.getIsOpen());
            item.setItemList(itemList);
            //相对路径按照“，”分隔
            item.setImages(itemList.stream().map(RepairRecordsItem::getImage).collect(Collectors.joining(",")));
        });
        return list;
    }

    /**
     * 根据当前用户和工单主表id查询当前在维修的项目信息
     *
     * @param ticketId 工单id
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<?>
     * @author vinjor-M
     * @date 13:35 2024/10/19
     **/
    @Override
    public CommonResult<?> selectNowRepairByTicketId(String ticketId) {
        return CommonResult.success(repairRecordsMapper.selectNowRepairByTicketId(ticketId));
    }
}
