package com.iformall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iformall.common.ErrorCode;
import com.iformall.common.IdWorker;
import com.iformall.common.Result;
import com.iformall.common.ResultData;
import com.iformall.domain.po.WxFloatingLayer;
import com.iformall.domain.vo.WxFloatingLayerVO;
import com.iformall.mapper.WxFloatingLayerMapper;
import com.iformall.service.WxFloatingLayerService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author gongbiao
 */
@Service
public class WxFloatingLayerServiceImpl implements WxFloatingLayerService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    WxFloatingLayerMapper wxFloatingLayerMapper;

    @Override
    public PageInfo<WxFloatingLayerVO> listAsPage(WxFloatingLayer record, Integer pageIndex, Integer pageSize) {
        return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> wxFloatingLayerMapper.findListVO(record));
    }

    @Override
    public WxFloatingLayer getById(Long id) {
        return wxFloatingLayerMapper.selectById(id);
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    @Override
    public ResultData saveOrUpdate(WxFloatingLayer wxFloatingLayer) {
        if (StringUtils.isEmpty(wxFloatingLayer.getCoverImg())) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "封面图不能为空");
        }
        if (StringUtils.isEmpty(wxFloatingLayer.getPagePath())) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "小程序跳转路径不能为空");
        }
        WxFloatingLayer wxFloatingLayerQuery = new WxFloatingLayer();
        wxFloatingLayerQuery.setTenantId(wxFloatingLayer.getTenantId());
        wxFloatingLayerQuery.setStatus(wxFloatingLayer.getStatus());
        Integer integer = wxFloatingLayerMapper.selectCount(new QueryWrapper<>(wxFloatingLayerQuery));
        if (integer > 0) {
            return new ResultData(ErrorCode.FLOATING_LAYER_FOUND);
        }
        Date date = new Date();
        if (wxFloatingLayer.getId() == null) {
            final IdWorker idWorker = IdWorker.get();
            wxFloatingLayer.setId(idWorker.nextId());
            wxFloatingLayer.setCreateTime(date);
            wxFloatingLayer.setUpdateTime(date);
            wxFloatingLayerMapper.insert(wxFloatingLayer);
        } else {
            wxFloatingLayer.setUpdateTime(date);
            wxFloatingLayerMapper.updateById(wxFloatingLayer);
        }
        return new ResultData(Result.SUCCESS, "操作成功");
    }

    @Override
    public ResultData updateStatus(WxFloatingLayer wxFloatingLayer) {
        wxFloatingLayer.setUpdateTime(new Date());
        wxFloatingLayerMapper.updateById(wxFloatingLayer);
        return new ResultData(Result.SUCCESS, "操作成功");

    }

}
