package org.jeecg.modules.bytedance.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.bytedance.common.entity.BytedanceOperationLog;
import org.jeecg.modules.bytedance.common.entity.MaterialInfo;
import org.jeecg.modules.bytedance.common.mapper.BytedanceOperationLogMapper;
import org.jeecg.modules.bytedance.common.mapper.MaterialInfoMapper;
import org.jeecg.modules.bytedance.common.service.IMaterialInfoService;
import org.jeecg.modules.bytedance.common.utils.Check;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 素材信息
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2019-10-11
 */
@Slf4j
@Service
public class MaterialInfoServiceImpl extends ServiceImpl<MaterialInfoMapper, MaterialInfo> implements IMaterialInfoService {

    @Resource
    private MaterialInfoMapper materialInfoMapper;

    @Resource
    private BytedanceOperationLogMapper bytedanceOperationLogMapper;


    @Override
    public MaterialInfo getParams(String signature, Integer status,Long projectId) {
        QueryWrapper<MaterialInfo>queryWrapper = new QueryWrapper<>();
        if(null!=signature&&!"".equals(signature.trim())){
            queryWrapper.eq("code",signature);
        }
        if(null!=status){
            queryWrapper.eq("status",status);
        }
        if(null!=projectId){
            queryWrapper.eq("project_id",projectId);
        }
        queryWrapper.orderByDesc("create_time").last("limit 1");
        return this.getOne(queryWrapper);
    }

    /**
     *
     * @description: 素材下线
     *
     * @param ids
     * @param offlineFlag
     * @return: org.jeecg.common.api.vo.Result
     * @author: zianY
     */
    @Override
    public Result updMaterialOfflineById(List<String> ids, Integer offlineFlag,String userId){
        ids.forEach(id ->{
            materialInfoMapper.updMaterialOfflineById(id,offlineFlag);
        });
        //添加操作记录
        BytedanceOperationLog operationLog = new BytedanceOperationLog();
        operationLog.setNewData(ids.toString());
        operationLog.setOldData(ids.toString());
        operationLog.setOperationType("素材下线");
        operationLog.setOperationContent(userId+"操作素材【"+ids+"】下线");
        operationLog.setCreateById(userId);
        bytedanceOperationLogMapper.insert(operationLog);
        return Result.successMsg("素材下线成功。", null);
    }

    @Override
    public MaterialInfo getDetilByMaterialId(String materialId){
        if(Check.isNull(materialId)){
            return null;
        }
        return this.getById(materialId);
    }
}





