package org.jeecg.modules.basedata.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Maps;
import org.jeecg.modules.basedata.entity.FdMilestoneTemplate;
import org.jeecg.modules.basedata.entity.FdMilestoneTemplateItem;
import org.jeecg.modules.basedata.mapper.FdMilestoneTemplateMapper;
import org.jeecg.modules.basedata.service.IFdMilestoneTemplateItemService;
import org.jeecg.modules.basedata.service.IFdMilestoneTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @Description: 里程碑模板
 * @author： jeecg-boot
 * @date：   2019-05-14
 * @version： V1.0
 */
@Service
@Transactional
public class FdMilestoneTemplateServiceImpl extends ServiceImpl<FdMilestoneTemplateMapper, FdMilestoneTemplate> implements IFdMilestoneTemplateService {

    @Resource
    FdMilestoneTemplateMapper fdMilestoneTemplateMapper;

    @Resource
    IFdMilestoneTemplateService fdMilestoneTemplateService;

    @Resource
    IFdMilestoneTemplateItemService fdMilestoneTemplateItemService;

    @Override
    public Map<String, Object> getPageFdMilestoneTemplate(Map<String, Object> mp) {
        Map<String,Object>map =  Maps.newHashMap();
        map.put("records",fdMilestoneTemplateMapper.getAllPage(mp));
        map.put("total",fdMilestoneTemplateMapper.getCount());
        map.put("current",mp.get("current"));
        return map;
    }

    /**
     * @Author gurui
     * @Description //TODO 根据ID查询
     * @Date 15:32 2019/5/14
     * @Param [id]
     * @return org.jeecg.modules.basedata.entity.FdMilestoneTemplate
     **/
    @Override
    public FdMilestoneTemplate getByIdFdMilestoneTemplate(String id) {
        return fdMilestoneTemplateMapper.getByIdMilestoneTemplate(id);
    }

    /**
     * @Author gurui
     * @Description //TODO 复制一条一样的模板
     * @Date 16:43 2019/7/5
     * @Param [id]
     * @return void
     **/
    @Override
    public void copy(String id) {
        //根据id获取模板
        FdMilestoneTemplate fdMilestoneTemplate = fdMilestoneTemplateMapper.selectById(id);
        fdMilestoneTemplate.setId("");
        fdMilestoneTemplate.setCreateTime(null);
        fdMilestoneTemplate.setName(fdMilestoneTemplate.getName()+"(副本)");
        fdMilestoneTemplateService.save(fdMilestoneTemplate);
        LambdaQueryWrapper<FdMilestoneTemplateItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FdMilestoneTemplateItem::getPkMilestoneTemplate,id);
        List<FdMilestoneTemplateItem> list = fdMilestoneTemplateItemService.list(queryWrapper);
        for (FdMilestoneTemplateItem fdMilestoneTemplateItem:list){
            fdMilestoneTemplateItem.setPid(fdMilestoneTemplateItem.getId());
            fdMilestoneTemplateItem.setId("");
            fdMilestoneTemplateItem.setCreateTime(null);
            fdMilestoneTemplateItem.setPkMilestoneTemplate(fdMilestoneTemplate.getId());
        }
        fdMilestoneTemplateItemService.saveBatch(list);
        getIdUpdatePid(list);
        fdMilestoneTemplateMapper.parentIdBatch(list);
    }

    void getIdUpdatePid(List<FdMilestoneTemplateItem> list){
        for (FdMilestoneTemplateItem fdMilestoneTemplateItem1:list){
            for (FdMilestoneTemplateItem fdMilestoneTemplateItem2:list){
                if (fdMilestoneTemplateItem1.getPid().equals(fdMilestoneTemplateItem2.getParentId())){
                    fdMilestoneTemplateItem2.setParentId(fdMilestoneTemplateItem1.getId());
                }
            }
        }
    }
}