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

import java.util.List;

/**
 * 管点信息采集数据表：TF_YWPN_PIPENODE_W
 *
 * @author fcl
 * @date Fri Apr 16 00:00:00 CST 2021
 */
@Service("tfYwpnPipenodeService")
public class TfYwpnPipenodeServiceImpl extends ServiceImpl<TfYwpnPipenodeMapper, TfYwpnPipenode> implements TfYwpnPipenodeService {

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

    @Override
    public ResultRespone saveTubePointsInfo( TfYwpnPipenode tfYwpnPipenode) {
        if (this.save(tfYwpnPipenode)) {
            String sid = gatherrecordService.getGlobalSid("629", tfYwpnPipenode.getId());
            tfYwpnPipenode.setSid(sid);
            update(Wrappers.<TfYwpnPipenode>lambdaUpdate().eq(TfYwpnPipenode::getId,tfYwpnPipenode.getId())
                    .set(TfYwpnPipenode::getSid,sid));

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

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

    @Override
    public ResultRespone removeNodeById(Long id) {
        TfYwpnPipenode node = this.getById(id);
        if (node != null ) {
            Integer count = pipeMapper.selectCount(Wrappers.<TfYwpnPipe>lambdaQuery().eq(TfYwpnPipe::getStartsid, node.getSid())
                    .or().eq(TfYwpnPipe::getEndsid, node.getSid()));
            if (count <= 0) {
                boolean ispublished = gatherrecordService.deleteByTableNameAndGatherId(CommonConstants.NODE_RECORD_TABLE_NAME, id);
                if (ispublished) {
                    return ResultRespone.success(this.removeById(id));
                } else {
                    return ResultRespone.failed("已提交数据不允许删除");
                }
            }else {
                return ResultRespone.failed("删除失败，管点已关联管线");
            }
        }
        return ResultRespone.failed("删除失败，管点信息获取失败");
    }

    @Override
    public List<TfYwpnPipenode> getPipenode(List<Long> pids) {
        return this.baseMapper.selectPipenode(pids);
    }

}
