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

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 消防栓信息采集数据表：TF_YWPN_HYDRANT_W
 *
 * @author fcl
 * @date Fri Apr 16 00:00:00 CST 2021
 */
@Service("tfYwpnHydrantService")
public class TfYwpnHydrantServiceImpl extends ServiceImpl<TfYwpnHydrantMapper, TfYwpnHydrant> implements TfYwpnHydrantService {

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


    @Override
    public ResultRespone saveFireHydrantInformation(TfYwpnHydrant tfYwpnHydrant) {
        if (this.save(tfYwpnHydrant)) {
            String sid = gatherrecordService.getGlobalSid("614", tfYwpnHydrant.getId());
            tfYwpnHydrant.setSid(sid);
            update(Wrappers.<TfYwpnHydrant>lambdaUpdate().eq(TfYwpnHydrant::getId,tfYwpnHydrant.getId())
                    .set(TfYwpnHydrant::getSid,sid));

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

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


    @Override
    public ResultRespone removeHydrantById(Long id) {
        boolean ispublished = gatherrecordService.deleteByTableNameAndGatherId(CommonConstants.HYDRANT_RECORD_TABLE_NAME, id);
        if (ispublished) {
            gatherrecordService.remove(Wrappers.<TfYwpnGatherrecord>lambdaQuery()
                    .eq(TfYwpnGatherrecord::getTableName,CommonConstants.HYDRANT_RECORD_TABLE_NAME)
                    .eq(TfYwpnGatherrecord::getGatherId,id));
            return ResultRespone.success(this.removeById(id));
        }
        return ResultRespone.failed("已提交数据不允许删除");
    }

}
