/*
 * 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.common.ftp.config.FtpUtil;
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.TfYwpnStationService;
import com.tofly.xrtymis.custom.utils.SaveFile;
import com.tofly.xrtymis.entity.TfYwpnGatherrecord;
import com.tofly.xrtymis.entity.TfYwpnStation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tofly.xrtymis.mapper.TfYwpnStationMapper;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 监测站信息采集数据表：TF_YWPN_STATION_W
 *
 * @author fcl
 * @date Fri Apr 16 00:00:00 CST 2021
 */
@Service("tfYwpnStationService")
public class TfYwpnStationServiceImpl extends ServiceImpl<TfYwpnStationMapper, TfYwpnStation> implements TfYwpnStationService {

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

    @Override
    public ResultRespone saveChickInformation(TfYwpnStation tfYwpnStation) {
        if (this.save(tfYwpnStation)) {
            String sid = gatherrecordService.getGlobalSid("611", tfYwpnStation.getId());
            tfYwpnStation.setSid(sid);
            update(Wrappers.<TfYwpnStation>lambdaUpdate().eq(TfYwpnStation::getId,tfYwpnStation.getId())
                    .set(TfYwpnStation::getSid,sid));

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

    @Override
    public ResultRespone updateStationById(List<MultipartFile> fileList,TfYwpnStation tfYwpnStation) {
        boolean ispublished = gatherrecordService.ispublished(CommonConstants.STATION_RECORD_TABLE_NAME, tfYwpnStation.getId());
        if (!ispublished) {
            StringBuilder filesb = saveFile.saveFiles(fileList,getById(tfYwpnStation.getId()).getFileIds(),tfYwpnStation.getFileIds());
            if(StringUtils.isEmpty(filesb)){
                this.update(new UpdateWrapper<TfYwpnStation>().lambda().set(TfYwpnStation::getFileIds,"").eq(TfYwpnStation::getId,tfYwpnStation.getId()));
            }
            tfYwpnStation.setFileIds(filesb.toString());

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

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

}
