/*
 * Author: Zev
 * 生成ServiceImpl
 */
package com.tofly.gpsboot.service.paln.task.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.system.IsPlanChildStateEnum;
import com.tofly.gpsboot.mapper.plan.PlanchildMapper;
import com.tofly.gpsboot.service.paln.task.PlanService;
import com.tofly.gpsboot.service.paln.task.PlanchildService;
import com.tofly.pojo.User;
import com.tofly.pojo.plan.Plan;
import com.tofly.pojo.plan.Planchild;
import com.tofly.pojo.plan.vo.PlanchildVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * 巡检子计划信息表：TF_YWPN_PLANCHILD_W
 *
 * @author admin
 * @date Thu Mar 18 00:00:00 CST 2021
 */
@Service("planchildService")
public class PlanchildServiceImpl extends ServiceImpl<PlanchildMapper, Planchild> implements PlanchildService {

    @Autowired
    private UserClent userClent;

    @Autowired
    private PlanService planService;

    @Override
    public List<PlanchildVo> viewConversion(List<Planchild> infoList) {
        List<PlanchildVo> infoVoList = new ArrayList<>();
        infoList.forEach(info ->{
            PlanchildVo baseInfo = JSONObject.parseObject(JSON.toJSONString(info),PlanchildVo.class);
            // 新增巡查人名称 子计划状态
            Long respUser = info.getRespUser();
            if (null != respUser && 0 != respUser) {
                User userAudit = userClent.getById(respUser).getResult();
                if(null!=userAudit){
                    baseInfo.setRespUserName(userAudit.getRealName());
                }

            }
            // 新增子计划开始时间与执行时间
            Plan plan = planService.getById(info.getPlanId());
            baseInfo.setPlanBegindate(plan.getPlanBegindate());
            baseInfo.setPlanEnddate(plan.getPlanEnddate());
            baseInfo.setPlanState(IsPlanChildStateEnum.getCodeForValue(info.getPlanState()));

            infoVoList.add(baseInfo);
        });

        return infoVoList.stream().sorted(Comparator.comparing(PlanchildVo::getPlanBegindate)).collect(Collectors.toList());
    }

    @Override
    public void generatePlanChildList(Set<Long> userIdList, Long planId){
        List<Planchild> planChildList = new ArrayList<>();
        userIdList.forEach(user -> {
            // 则生成对应计划的子计划
            Planchild planchild = new Planchild();
            planchild.setPlanId(planId);
            planchild.setRespUser(user);
            planchild.setPlanState(IsPlanChildStateEnum.PLAN_STATE_NOT_START.getCode());
            planChildList.add(planchild);
        });
        this.saveBatch(planChildList);
    }

    @Override
    public String getPlanChildState(Long planId,Long userId){
       return this.getOne(new QueryWrapper<Planchild>().lambda().eq(Planchild::getPlanId,planId).eq(Planchild::getRespUser,userId)).getPlanState();
    }

    @Override
    public void updateStateOver() {
        baseMapper.updateStateOver();
    }

    @Override
    public List<Planchild> getChileInfo(Set<Long> planId, Long userId) {
        return baseMapper.getChiledInfo(planId, userId);
    }

}
