package com.sptg.ad.biz.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sptg.ad.api.dto.AdMaterialDto;
import com.sptg.ad.api.entity.AdContent;
import com.sptg.ad.api.entity.AdMaterial;
import com.sptg.ad.biz.mapper.AdMaterialMapper;
import com.sptg.ad.biz.service.AdMaterialServiceInterface;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.common.core.constant.SymbolConstant;
import com.sptg.common.core.util.CommonUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Arrays;
import java.util.List;

@Service
public class AdMaterialServiceImpl extends ServiceImpl<AdMaterialMapper, AdMaterial> implements AdMaterialServiceInterface {


    @Override
    public Page<AdMaterial> showAdMaterialList(AdMaterialDto adMaterialDto) {
        //排序字段 时间降序
//        adAdsenseDto.getPage().setOrderByField("l_create_date");
//        adAdsenseDto.getPage().setAsc(false);

        //构建查询条件
        Wrapper<AdMaterial> wrapper = new EntityWrapper<>();

        wrapper.notIn("i_material_status", OperationConstant.MINUS_ONE);

        if(StringUtils.isNotBlank(adMaterialDto.getMaterialName())){
            wrapper.like("s_material_name", adMaterialDto.getMaterialName());
        }
        if(StringUtils.isNotBlank(adMaterialDto.getMaterialType()) && !adMaterialDto.getMaterialType().equals("all")){
            wrapper.like("s_material_type", adMaterialDto.getMaterialType());
        }
        if(adMaterialDto.getMaterialStatus() ==1){
            wrapper.eq("i_material_status", adMaterialDto.getMaterialStatus());
        }else if(adMaterialDto.getMaterialStatus() == 2){
            wrapper.eq("i_material_status", adMaterialDto.getMaterialStatus());
        }
        //xml文件sql查询
        Page<AdMaterial> page = adMaterialDto.getPage();
        page.setRecords(this.baseMapper.showAdMaterialList(wrapper,adMaterialDto.getPage()));
        return page;
    }

    @Override
    public ResultBean<Boolean> addAdMaterial(AdMaterial adMaterial) {
        int materialNumber = this.getAllMaterialCount();
        adMaterial.setMaterialNumber(SymbolConstant.SC_NO+ CommonUtil.autoZeroFilling(6,materialNumber));
        boolean result = this.insertOrUpdate(adMaterial);
        if(result){
            return new ResultBean<>(ResultEnum.SUCCESS);
        }else{
            return new ResultBean<>("200","操作失败");
        }
    }

    @Override
    public ResultBean<Boolean> deleteAdMaterial(String ids) {
        String[] id = ids.split(",");
        if(id!=null && ids.length()>0){
            List<String> list = Arrays.asList(id);
            Wrapper<AdMaterial> wrapper = new EntityWrapper<>();
            wrapper.in("l_id", list);
            //需要修改状态的广告素材
            AdMaterial adMaterial = new AdMaterial();
            adMaterial.setStatus(OperationConstant.MINUS_ONE);
            return new ResultBean<>(ResultEnum.SUCCESS, this.update(adMaterial,wrapper));
        }
        return new ResultBean<>(ResultEnum.ERROR);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public ResultBean<Boolean> deleteAdMaterialList(AdMaterialDto adMaterialDto) {
        List<String> ids = adMaterialDto.getIds();
        for(String id : ids){
            //  获取账号信息
            AdMaterial adMaterial = this.selectById(Long.valueOf(id));
            Assert.notNull(adMaterial,"记录不存在,请核实后重新操作");
            //  标记为删除状态
            adMaterial.setStatus(OperationConstant.MINUS_ONE);
            this.updateById(adMaterial);
        }
        return new ResultBean<>(ResultEnum.SUCCESS,true);
    }

    @Override
    public int getAllMaterialCount() {
        return this.selectCount(null);
    }

    @Override
    public AdMaterial findAdMaterialById(Long id) {
        return this.selectById(id);
    }
}
