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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.basedata.entity.FdQuotationTemplate;
import org.jeecg.modules.basedata.entity.FdQuotationTemplateItem;
import org.jeecg.modules.basedata.entity.FdQuotationTemplateItemTree;
import org.jeecg.modules.basedata.mapper.FdQuotationTemplateItemMapper;
import org.jeecg.modules.basedata.mapper.FdQuotationTemplateMapper;
import org.jeecg.modules.basedata.service.IFdQuotationTemplateItemService;
import org.jeecg.modules.basedata.service.IFdQuotationTemplateService;
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.ArrayList;
import java.util.List;

/**
 * @Description: 报价单模板
 * @author： jeecg-boot
 * @date：   2019-05-16
 * @version： V1.0
 */
@Service
@Transactional
public class FdQuotationTemplateServiceImpl extends ServiceImpl<FdQuotationTemplateMapper, FdQuotationTemplate> implements IFdQuotationTemplateService {

    @Resource
    FdQuotationTemplateMapper fdQuotationTemplateMapper;
    @Resource
    IFdQuotationTemplateService fdQuotationTemplateService;
    @Resource
    IFdQuotationTemplateItemService fdQuotationTemplateItemService;
    @Resource
    FdQuotationTemplateItemMapper fdQuotationTemplateItemMapper;


    @Override
    public IPage<FdQuotationTemplate> findByPage(Page<FdQuotationTemplate> page, QueryWrapper queryWrapper) {
        return fdQuotationTemplateMapper.findByPage(page,queryWrapper);
    }

    /***
     * @Title:根据多个id查询模板,并根据模板类型分组
            * @Description:TODO
            * @param
            * @return java.util.List<org.jeecg.modules.basedata.entity.FdQuotationTemplate>
            * @exception 
            * @author 陈创
            * @date 2019/6/11 16:46
            * @updatehistory:
     *   2019/6/11 16:46 新增
            */
    @Override
    public List<FdQuotationTemplate> getbyIdBatch(List<String> ids) {
        //根据多个id查询模板的集合
        List<FdQuotationTemplate> quotationTemplateList=fdQuotationTemplateMapper.selectBatchIds(ids);
        return quotationTemplateList;
    }

    /**
     * @Author gurui
     * @Description //TODO 复制一条一样的模板
     * @Date 16:45 2019/7/5
     * @Param [id]
     * @return void
     **/
    @Override
    public  List<FdQuotationTemplateItem> copy(String id) {
        FdQuotationTemplate fdQuotationTemplate = fdQuotationTemplateMapper.selectById(id);
        fdQuotationTemplate.setId("");
        fdQuotationTemplate.setCreateTime(null);
        fdQuotationTemplate.setName(fdQuotationTemplate.getName()+"(副本)");
        fdQuotationTemplateService.save(fdQuotationTemplate);
        LambdaQueryWrapper<FdQuotationTemplateItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FdQuotationTemplateItem::getPkQuotationTemplate,id);
        List<FdQuotationTemplateItem> list = fdQuotationTemplateItemService.list(queryWrapper);
        for (FdQuotationTemplateItem fdQuotationTemplateItem:list){
            fdQuotationTemplateItem.setPid(fdQuotationTemplateItem.getId());
            fdQuotationTemplateItem.setId("");
            fdQuotationTemplateItem.setCreateTime(null);
            fdQuotationTemplateItem.setPkQuotationTemplate(fdQuotationTemplate.getId());
        }
        fdQuotationTemplateItemService.saveBatch(list);
        getIdUpdatePid(list);
        fdQuotationTemplateMapper.parentIdBatch(list);
        return list;
    }

    public void getIdUpdatePid(List<FdQuotationTemplateItem> list ){
        for(FdQuotationTemplateItem fdQuotationTemplateIte:list){
            for(FdQuotationTemplateItem fdQuotationTemplateItem2:list){
                if(fdQuotationTemplateIte.getPid().equals(fdQuotationTemplateItem2.getParentId())){
                    fdQuotationTemplateItem2.setParentId(fdQuotationTemplateIte.getId());
                }
            }

        }
    }

    /**
     * @Author gurui
     * @Description //TODO 删除模板同时删除明细
     * @Date 20:11 2019/7/16
     * @Param [id]
     * @return boolean
     **/
    @Override
    public boolean deleteAll(String id) {
        boolean result = true;
        try {
            LambdaQueryWrapper<FdQuotationTemplateItem> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(FdQuotationTemplateItem::getPkQuotationTemplate,id);
            fdQuotationTemplateItemService.remove(queryWrapper);
            fdQuotationTemplateService.removeById(id);
        }catch (Exception e){
            e.printStackTrace();
            result = false;
        }
        return result;
    }
}
