/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.xrtymis.custom.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.xrtymis.config.FilePathBean;
import com.tofly.xrtymis.custom.commons.CommonConstants;
import com.tofly.xrtymis.custom.commons.Constate;
import com.tofly.xrtymis.custom.service.TfYwpnGatherrecordService;
import com.tofly.xrtymis.custom.service.TfYwpnManholeService;
import com.tofly.xrtymis.custom.utils.SaveFile;
import com.tofly.xrtymis.entity.TfYwpnGatherrecord;
import com.tofly.xrtymis.entity.TfYwpnManhole;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tofly.xrtymis.mapper.TfYwpnManholeMapper;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 检查井信息采集数据表：TF_YWPN_MANHOLE_W
 *
 * @author fcl
 * @date Fri Apr 16 00:00:00 CST 2021
 */
@Service("tfYwpnManholeService")
public class TfYwpnManholeServiceImpl extends ServiceImpl<TfYwpnManholeMapper, TfYwpnManhole> implements TfYwpnManholeService {

    @Autowired
    private SaveFile saveFile;
    @Autowired
    private FilePathBean filePathBean;
    @Autowired
    private TfYwpnGatherrecordService gatherrecordService;

    @Override
    public ResultRespone saveCheckWell( TfYwpnManhole tfYwpnManhole) {
        if (this.save(tfYwpnManhole)) {
            String sid = gatherrecordService.getGlobalSid("615", tfYwpnManhole.getId());
            tfYwpnManhole.setSid(sid);
            update(Wrappers.<TfYwpnManhole>lambdaUpdate().eq(TfYwpnManhole::getId,tfYwpnManhole.getId())
                    .set(TfYwpnManhole::getSid,sid));

            Integer count = gatherrecordService.getBaseMapper().selectCount(Wrappers.<TfYwpnGatherrecord>lambdaQuery()
                    .eq(TfYwpnGatherrecord::getTableName, CommonConstants.MANHOLE_RECORD_TABLE_NAME)
                    .eq(TfYwpnGatherrecord::getGatherId, tfYwpnManhole.getId()));
            if (count <= 0 ) {
                TfYwpnGatherrecord gatherrecord = new TfYwpnGatherrecord();
                gatherrecord.setGatherType("615");
                gatherrecord.setTableName(CommonConstants.MANHOLE_RECORD_TABLE_NAME);
                gatherrecord.setGatherId(tfYwpnManhole.getId());
                gatherrecord.setGatherTime(tfYwpnManhole.getCreateTime());
                gatherrecord.setAuditUser(tfYwpnManhole.getCreateUser());
                gatherrecord.setState(Constate.NO);
                gatherrecord.setIsread(Constate.NO);
                gatherrecord.setIspublish(Constate.NO);
                gatherrecordService.save(gatherrecord);
            }
            return ResultRespone.success(tfYwpnManhole);
        }
        return ResultRespone.failed();
    }

    @Override
    public ResultRespone updateManholeById(List<MultipartFile> fileList,TfYwpnManhole tfYwpnManhole) {
        boolean ispublished = gatherrecordService.ispublished(CommonConstants.MANHOLE_RECORD_TABLE_NAME, tfYwpnManhole.getId());
        if (!ispublished) {
            StringBuilder filesb = saveFile.saveFiles(fileList,getById(tfYwpnManhole.getId()).getFileIds(),tfYwpnManhole.getFileIds());
            if(StringUtils.isEmpty(filesb)){
                this.update(new UpdateWrapper<TfYwpnManhole>().lambda().set(TfYwpnManhole::getFileIds,"").eq(TfYwpnManhole::getId,tfYwpnManhole.getId()));
            }
            tfYwpnManhole.setFileIds(filesb.toString());
            return ResultRespone.success(this.updateById(tfYwpnManhole));
        }
        return ResultRespone.failed("已提交数据不允许修改");
    }



    @Override
    public ResultRespone removeManholeById(Long id) {
        boolean ispublished = gatherrecordService.deleteByTableNameAndGatherId(CommonConstants.MANHOLE_RECORD_TABLE_NAME, id);
        if (ispublished) {
            return ResultRespone.success(this.removeById(id));
        }
        return ResultRespone.failed("已提交数据不允许删除");
    }

}
