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

import java.util.List;

/**
 * 非控制阀信息采集数据表：TF_YWPN_NONVALVE_W
 *
 * @author fcl
 * @date Fri Apr 16 00:00:00 CST 2021
 */
@Service("tfYwpnNonvalveService")
public class TfYwpnNonvalveServiceImpl extends ServiceImpl<TfYwpnNonvalveMapper, TfYwpnNonvalve> implements TfYwpnNonvalveService {

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

    @Override
    public ResultRespone saveControlValve(TfYwpnNonvalve tfYwpnNonvalve) {
        if (this.save(tfYwpnNonvalve)) {
            String sid = gatherrecordService.getGlobalSid("613", tfYwpnNonvalve.getId());
            tfYwpnNonvalve.setSid(sid);
            update(Wrappers.<TfYwpnNonvalve>lambdaUpdate().eq(TfYwpnNonvalve::getId,tfYwpnNonvalve.getId())
                    .set(TfYwpnNonvalve::getSid,sid));

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

    @Override
    public ResultRespone updateNonvalveById(List<MultipartFile> fileList, TfYwpnNonvalve tfYwpnNonvalve) {

        boolean ispublished = gatherrecordService.ispublished(CommonConstants.NONVALVE_RECORD_TABLE_NAME, tfYwpnNonvalve.getId());
        if (!ispublished) {

            StringBuilder filesb = saveFile.saveFiles(fileList,getById(tfYwpnNonvalve.getId()).getFileIds(),tfYwpnNonvalve.getFileIds());
            if(StringUtils.isEmpty(filesb)){
                this.update(new UpdateWrapper<TfYwpnNonvalve>().lambda().set(TfYwpnNonvalve::getFileIds,"").eq(TfYwpnNonvalve::getId,tfYwpnNonvalve.getId()));

            }
            tfYwpnNonvalve.setFileIds(filesb.toString());
            return ResultRespone.success(this.updateById(tfYwpnNonvalve));
        }
        return ResultRespone.failed("已提交数据不允许修改");
    }

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

}
