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

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.TfYwpnPipeService;
import com.tofly.xrtymis.custom.service.TfYwpnPipenodeService;
import com.tofly.xrtymis.custom.utils.SaveFile;
import com.tofly.xrtymis.entity.TfYwpnGatherrecord;
import com.tofly.xrtymis.entity.TfYwpnPipe;
import com.tofly.xrtymis.entity.TfYwpnPipenode;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tofly.xrtymis.mapper.TfYwpnPipeMapper;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 管线信息采集数据表：TF_YWPN _PIPE_W
 *
 * @author fcl
 * @date Fri Apr 16 00:00:00 CST 2021
 */
@Service("tfYwpnPipeService")
public class TfYwpnPipeServiceImpl extends ServiceImpl<TfYwpnPipeMapper, TfYwpnPipe> implements TfYwpnPipeService {

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

    @Override
    public ResultRespone savePipeline(TfYwpnPipe tfYwpnPipe) {
        if (this.save(tfYwpnPipe)) {
            String sid = gatherrecordService.getGlobalSid("609", tfYwpnPipe.getId());
            tfYwpnPipe.setSid(sid);
            update(Wrappers.<TfYwpnPipe>lambdaUpdate().eq(TfYwpnPipe::getId,tfYwpnPipe.getId())
                    .set(TfYwpnPipe::getSid,sid));

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

    @Override
    public ResultRespone updatePipeById(List<MultipartFile> fileList, TfYwpnPipe tfYwpnPipe) {
        if (null != fileList && fileList.size() > 0) {
            String files = getById(tfYwpnPipe.getId()).getFileIds();
            if (StringUtils.isNotBlank(files)) {
                saveFile.removeFileById(files);
            }
            StringBuilder fileIds = saveFile.saveFileList(fileList, filePathBean.getSbxxcj(), CommonConstants.PIPE_RECORD_TABLE_NAME);
            if (fileIds.length() > 0) {
                tfYwpnPipe.setFileIds(fileIds.substring(0, fileIds.length() - 1));
            }
        }

        boolean ispublished = gatherrecordService.ispublished(CommonConstants.PIPE_RECORD_TABLE_NAME, tfYwpnPipe.getId());
        if (!ispublished) {
            return ResultRespone.success(this.updateById(tfYwpnPipe));
        }
        return ResultRespone.failed("已提交数据不允许修改");
    }

    @Override
    public ResultRespone removePipeById(Long id) {
        boolean ispublished = gatherrecordService.deleteByTableNameAndGatherId(CommonConstants.PIPE_RECORD_TABLE_NAME, id);
        if (ispublished) {
            TfYwpnPipe pipe = this.getById(id);
            if (ObjectUtils.isEmpty(pipe)) {
                return ResultRespone.failed("数据不存在，请重试");
            }
            // 删除关联起点 终点
            // 增加管点删除校验 测试中发现，如:1、2两条线，1的终点是2的起点，直接删除后，在对管线2进行相关操作时出现逻辑错误
            Integer startCount = this.getBaseMapper().selectCount(Wrappers.<TfYwpnPipe>lambdaQuery().eq(TfYwpnPipe::getStartsid, pipe.getStartsid())
                    .or().eq(TfYwpnPipe::getEndsid, pipe.getStartsid()));
            if (1 == startCount) {
                Long startNodeId = nodeService.getOne(Wrappers.<TfYwpnPipenode>lambdaQuery().eq(TfYwpnPipenode::getSid, pipe.getStartsid())).getId();
                nodeService.remove(Wrappers.<TfYwpnPipenode>lambdaQuery().eq(TfYwpnPipenode::getSid,pipe.getStartsid()));
                gatherrecordService.deleteByTableNameAndGatherId(CommonConstants.NODE_RECORD_TABLE_NAME,startNodeId);
            }

            Integer endCount = this.getBaseMapper().selectCount(Wrappers.<TfYwpnPipe>lambdaQuery().eq(TfYwpnPipe::getStartsid, pipe.getEndsid())
                    .or().eq(TfYwpnPipe::getEndsid, pipe.getEndsid()));
            if (1 == endCount) {
                Long endNodeId = nodeService.getOne(Wrappers.<TfYwpnPipenode>lambdaQuery().eq(TfYwpnPipenode::getSid, pipe.getStartsid())).getId();
                nodeService.remove(Wrappers.<TfYwpnPipenode>lambdaQuery().eq(TfYwpnPipenode::getSid,pipe.getEndsid()));
                gatherrecordService.deleteByTableNameAndGatherId(CommonConstants.NODE_RECORD_TABLE_NAME,endNodeId);

            }
            return ResultRespone.success(this.removeById(id));
        }
        return ResultRespone.failed("已提交数据不允许删除");
    }
}
