/*
 * 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.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tofly.api.message.MessageClient;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.oauth.util.SecurityUtils;
import com.tofly.pojo.message.Messagepush;
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.dto.TfYwpnValveDTO;
import com.tofly.xrtymis.custom.service.FileService;
import com.tofly.xrtymis.custom.service.GddataService;
import com.tofly.xrtymis.custom.service.TfYwpnGatherrecordService;
import com.tofly.xrtymis.custom.service.TfYwpnValveService;
import com.tofly.xrtymis.custom.utils.SaveFile;
import com.tofly.xrtymis.entity.Gddata;
import com.tofly.xrtymis.entity.TfYwpnGatherrecord;
import com.tofly.xrtymis.entity.TfYwpnStation;
import com.tofly.xrtymis.entity.TfYwpnValve;
import com.tofly.xrtymis.mapper.FileMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tofly.xrtymis.mapper.TfYwpnValveMapper;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 控制阀信息采集数据表：TF_YWPN_VALVE_W
 *
 * @author fcl
 * @date Fri Apr 16 00:00:00 CST 2021
 */
@Service("tfYwpnValveService")
public class TfYwpnValveServiceImpl extends ServiceImpl<TfYwpnValveMapper, TfYwpnValve> implements TfYwpnValveService {
    @Autowired
    private SaveFile saveFile;
    @Autowired
    private FilePathBean filePathBean;
    @Autowired
    private TfYwpnGatherrecordService gatherrecordService;

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

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

    @Override
    public ResultRespone updateValveById(List<MultipartFile> fileList,TfYwpnValve tfYwpnValve) {

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

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

}
