/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.gpsboot.service.maintain.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.tofly.api.UserClent;
import com.tofly.gpsboot.common.Constant;
import com.tofly.gpsboot.common.system.IsReadEnum;
import com.tofly.gpsboot.common.system.IsRespectEnum;
import com.tofly.gpsboot.common.system.OrderStateEnum;
import com.tofly.pojo.User;
import com.tofly.pojo.maintain.vo.RepairuserVo;
import com.tofly.gpsboot.mapper.maintain.RepairuserMapper;
import com.tofly.gpsboot.service.maintain.RepairuserService;
import com.tofly.pojo.maintain.Repairuser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * 抢维修工单个人任务信息表
 *
 * @author admin
 * @date Tue Apr 06 00:00:00 CST 2021
 */
@Service("repairuserService")
public class RepairuserServiceImpl extends ServiceImpl<RepairuserMapper, Repairuser> implements RepairuserService {

    @Autowired
    private UserClent userClent;

    @Override
    public List<RepairuserVo> viewConversion(List<Repairuser> infoList) {
        List<RepairuserVo> infoVoList = new ArrayList<>();
        infoList.forEach(info -> {
            RepairuserVo baseInfo = JSONObject.parseObject(JSON.toJSONString(info), RepairuserVo.class);
            // 需要增加维修人名称/协同处理人字符串/ 转换 是否确认/是否返工/工单状态
            User result = userClent.getById(info.getRespUser()).getResult();
            if(null!=result){
                baseInfo.setRespUserName(result.getRealName());

            }
            baseInfo.setTeamUserName(this.getTeamUserNames(info.getId()));
            baseInfo.setIsread(IsReadEnum.getCodeForValue(info.getIsread()));
            baseInfo.setIsrepeat(IsRespectEnum.getCodeForValue(info.getIsrepeat()));
            baseInfo.setState(OrderStateEnum.getCodeForValue(info.getState()));
            infoVoList.add(baseInfo);
        });
        return infoVoList;
    }

    @Override
    public String getRespUserName(Long repairId) {
        List<Repairuser> list = list(new QueryWrapper<Repairuser>().lambda().eq(Repairuser::getRepairId, repairId));
        if(null != list && list.size()>0){
                Long respUser = list.get(0).getRespUser();
                if(null!=respUser){
                    User result = userClent.getById(respUser).getResult();
                    if(null!=result){
                        return result.getRealName();
                    }

                }

        }
        return "";
    }

    @Override
    public String getTeamUserNames(Long id) {
        List<Repairuser> list = list(new QueryWrapper<Repairuser>().lambda().eq(Repairuser::getId, id));
        if(null !=list && list.size() >0){
            String teamUser = list.get(0).getTeamUser();
                    StringBuffer str = new StringBuffer("");
            if (!StringUtils.isEmpty(teamUser)) {
                List<String> stringList = Arrays.asList(teamUser.split(Constant.CSV));
                stringList.forEach(info -> {
                    User result = userClent.getById(Long.valueOf(info)).getResult();
                    if(null!=result){
                        str.append(result.getRealName()).append(Constant.CSV);
                    }


                });
                return str.deleteCharAt(str.length() - 1).toString();
            }
        }

        return "";
    }
}
