/*
 * Author: Zev
 * 生成ServiceImpl
 */
package com.tofly.gpsboot.service.config.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.tofly.gpsboot.common.Constant;
import com.tofly.pojo.plan.dto.InspecttypeDto;
import com.tofly.pojo.plan.vo.InspecttypeVo;
import com.tofly.gpsboot.exception.BusinessException;
import com.tofly.gpsboot.mapper.plan.InspecttypeMapper;
import com.tofly.pojo.plan.Inspecttype;
import com.tofly.gpsboot.service.config.InspecttypeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 巡检类型配置信息表：TF_YWPN_INSPECTTYPE_W
 *
 * @author admin
 * @date Thu Mar 18 00:00:00 CST 2021
 */
@Service("inspecttypeService")
public class InspecttypeServiceImpl extends ServiceImpl<InspecttypeMapper, Inspecttype> implements InspecttypeService {


    @Override
    public Page<Inspecttype> getInspectTypePage(Page page, InspecttypeDto parDto) {
        Inspecttype inspecttype = JSON.parseObject(JSON.toJSONString(parDto), Inspecttype.class);
        QueryWrapper<Inspecttype> wrapper = new QueryWrapper();
        String name = parDto.getName();
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("NAME", name);
            inspecttype.setName(null);
        }
        wrapper.eq("FLAG","1");
        inspecttype.setFlag(null);
        wrapper.setEntity(inspecttype);
        return this.page(page, wrapper);
    }

    @Override
    public String getInspectTypeName(List<Long> ids) {
        StringBuilder stringBuilder = new StringBuilder("");
        ids.forEach(info -> {
            stringBuilder.append(this.getById(info).getName()).append(Constant.CSV);
        });

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteInspectTypeInfo(String idLists) throws BusinessException {
        // 删除 flag置为0
        List<Long> longList = Arrays.stream(idLists.split(Constant.CSV)).map(s -> Long.parseLong(s.trim()))
                .collect(Collectors.toList());
        List<Inspecttype> inspecttypes = this.listByIds(longList);
        inspecttypes.forEach(info ->info.setFlag("0"));
        this.updateBatchById(inspecttypes);


    }

    @Override
    public List<Inspecttype> getByGroupInspectType(String groupCode) {

        return list(new QueryWrapper<Inspecttype>().lambda().eq(Inspecttype::getGroupCode,groupCode).eq(Inspecttype::getFlag,"1"));
    }
}
