Quellcode durchsuchen

自动投放重构清洗账户素材

zhaoxian vor 3 Jahren
Ursprung
Commit
50db5dd4a0

+ 265 - 0
jeecg-boot-module-system/src/main/java/cn/com/ctop/kuaishou/modules/ai/controller/AiKuaishouAccountAutoVideoController.java

@@ -0,0 +1,265 @@
+package cn.com.ctop.kuaishou.modules.ai.controller;
+
+import cn.com.ctop.kuaishou.modules.ai.entity.AiKuaishouAccountAutoVideo;
+import cn.com.ctop.kuaishou.modules.ai.service.IAiKuaishouAccountAutoVideoService;
+import com.alibaba.fastjson.JSON;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import lombok.extern.slf4j.Slf4j;
+import org.jeecg.common.api.vo.Result;
+import org.jeecg.common.system.query.QueryGenerator;
+import org.jeecg.common.util.oConvertUtils;
+import org.jeecgframework.poi.excel.ExcelImportUtil;
+import org.jeecgframework.poi.excel.def.NormalExcelConstants;
+import org.jeecgframework.poi.excel.entity.ExportParams;
+import org.jeecgframework.poi.excel.entity.ImportParams;
+import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.DeleteMapping;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.PutMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.multipart.MultipartFile;
+import org.springframework.web.multipart.MultipartHttpServletRequest;
+import org.springframework.web.servlet.ModelAndView;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * 自动投放账户——素材
+ *
+ * @author jeecg-boot
+ * @version V1.0
+ * @date 2021-10-20
+ */
+@Slf4j
+@Api(tags = "自动投放账户——素材")
+@RestController
+@RequestMapping("/auto/aiKuaishouAccountAutoVideo")
+public class AiKuaishouAccountAutoVideoController {
+    @Autowired
+    private IAiKuaishouAccountAutoVideoService aiKuaishouAccountAutoVideoService;
+
+    /**
+     * 分页列表查询
+     *
+     * @param aiKuaishouAccountAutoVideo
+     * @param pageNo
+     * @param pageSize
+     * @param req
+     * @return
+     */
+    @ApiOperation(value = "自动投放账户——素材-分页列表查询", notes = "自动投放账户——素材-分页列表查询")
+    @GetMapping(value = "/list")
+    public Result<IPage<AiKuaishouAccountAutoVideo>> queryPageList(AiKuaishouAccountAutoVideo aiKuaishouAccountAutoVideo,
+                                                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
+                                                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
+                                                                   HttpServletRequest req) {
+        Result<IPage<AiKuaishouAccountAutoVideo>> result = new Result<>();
+        QueryWrapper<AiKuaishouAccountAutoVideo> queryWrapper = QueryGenerator.initQueryWrapper(aiKuaishouAccountAutoVideo, req.getParameterMap());
+        Page<AiKuaishouAccountAutoVideo> page = new Page<AiKuaishouAccountAutoVideo>(pageNo, pageSize);
+        IPage<AiKuaishouAccountAutoVideo> pageList = aiKuaishouAccountAutoVideoService.page(page, queryWrapper);
+        result.setSuccess(true);
+        result.setResult(pageList);
+        return result;
+    }
+
+    /**
+     * 自动投放重构清洗账户素材
+     */
+    @GetMapping(value = "/insertAutoVideo")
+    public Result<AiKuaishouAccountAutoVideo> insertAutoVideo(Long accountId) {
+        Result<AiKuaishouAccountAutoVideo> result = new Result<>();
+        try {
+            aiKuaishouAccountAutoVideoService.insertAutoVideo(accountId);
+            result.success("添加成功!");
+        } catch (Exception e) {
+            log.error(e.getMessage(), e);
+            result.error500("操作失败");
+        }
+        return result;
+    }
+
+    /**
+     * 添加
+     *
+     * @param aiKuaishouAccountAutoVideo
+     * @return
+     */
+    @ApiOperation(value = "自动投放账户——素材-添加", notes = "自动投放账户——素材-添加")
+    @PostMapping(value = "/add")
+    public Result<AiKuaishouAccountAutoVideo> add(@RequestBody AiKuaishouAccountAutoVideo aiKuaishouAccountAutoVideo) {
+        Result<AiKuaishouAccountAutoVideo> result = new Result<>();
+        try {
+            aiKuaishouAccountAutoVideoService.save(aiKuaishouAccountAutoVideo);
+            result.success("添加成功!");
+        } catch (Exception e) {
+            log.error(e.getMessage(), e);
+            result.error500("操作失败");
+        }
+        return result;
+    }
+
+    /**
+     * 编辑
+     *
+     * @param aiKuaishouAccountAutoVideo
+     * @return
+     */
+    @ApiOperation(value = "自动投放账户——素材-编辑", notes = "自动投放账户——素材-编辑")
+    @PutMapping(value = "/edit")
+    public Result<AiKuaishouAccountAutoVideo> edit(@RequestBody AiKuaishouAccountAutoVideo aiKuaishouAccountAutoVideo) {
+        Result<AiKuaishouAccountAutoVideo> result = new Result<AiKuaishouAccountAutoVideo>();
+        AiKuaishouAccountAutoVideo aiKuaishouAccountAutoVideoEntity = aiKuaishouAccountAutoVideoService.getById(aiKuaishouAccountAutoVideo.getId());
+        if (aiKuaishouAccountAutoVideoEntity == null) {
+            result.error500("未找到对应实体");
+        } else {
+            boolean ok = aiKuaishouAccountAutoVideoService.updateById(aiKuaishouAccountAutoVideo);
+            if (ok) {
+                result.success("修改成功!");
+            }
+        }
+
+        return result;
+    }
+
+    /**
+     * 通过id删除
+     *
+     * @param id
+     * @return
+     */
+    @ApiOperation(value = "自动投放账户——素材-通过id删除", notes = "自动投放账户——素材-通过id删除")
+    @DeleteMapping(value = "/delete")
+    public Result<?> delete(@RequestParam(name = "id") String id) {
+        try {
+            aiKuaishouAccountAutoVideoService.removeById(id);
+        } catch (Exception e) {
+            log.error("删除失败", e.getMessage());
+            return Result.error("删除失败!");
+        }
+        return Result.ok("删除成功!");
+    }
+
+    /**
+     * 批量删除
+     *
+     * @param ids
+     * @return
+     */
+    @ApiOperation(value = "自动投放账户——素材-批量删除", notes = "自动投放账户——素材-批量删除")
+    @DeleteMapping(value = "/deleteBatch")
+    public Result<AiKuaishouAccountAutoVideo> deleteBatch(@RequestParam(name = "ids") String ids) {
+        Result<AiKuaishouAccountAutoVideo> result = new Result<>();
+        if (ids == null || "".equals(ids.trim())) {
+            result.error500("参数不识别!");
+        } else {
+            this.aiKuaishouAccountAutoVideoService.removeByIds(Arrays.asList(ids.split(",")));
+            result.success("删除成功!");
+        }
+        return result;
+    }
+
+    /**
+     * 通过id查询
+     *
+     * @param id
+     * @return
+     */
+    @ApiOperation(value = "自动投放账户——素材-通过id查询", notes = "自动投放账户——素材-通过id查询")
+    @GetMapping(value = "/queryById")
+    public Result<AiKuaishouAccountAutoVideo> queryById(@RequestParam(name = "id", required = true) String id) {
+        Result<AiKuaishouAccountAutoVideo> result = new Result<>();
+        AiKuaishouAccountAutoVideo aiKuaishouAccountAutoVideo = aiKuaishouAccountAutoVideoService.getById(id);
+        if (aiKuaishouAccountAutoVideo == null) {
+            result.error500("未找到对应实体");
+        } else {
+            result.setResult(aiKuaishouAccountAutoVideo);
+            result.setSuccess(true);
+        }
+        return result;
+    }
+
+    /**
+     * 导出excel
+     *
+     * @param request
+     * @param response
+     */
+    @RequestMapping(value = "/exportXls")
+    public ModelAndView exportXls(HttpServletRequest request, HttpServletResponse response) {
+        // Step.1 组装查询条件
+        QueryWrapper<AiKuaishouAccountAutoVideo> queryWrapper = null;
+        try {
+            String paramsStr = request.getParameter("paramsStr");
+            if (oConvertUtils.isNotEmpty(paramsStr)) {
+                String deString = URLDecoder.decode(paramsStr, "UTF-8");
+                AiKuaishouAccountAutoVideo aiKuaishouAccountAutoVideo = JSON.parseObject(deString, AiKuaishouAccountAutoVideo.class);
+                queryWrapper = QueryGenerator.initQueryWrapper(aiKuaishouAccountAutoVideo, request.getParameterMap());
+            }
+        } catch (UnsupportedEncodingException e) {
+            e.printStackTrace();
+        }
+
+        //Step.2 AutoPoi 导出Excel
+        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
+        List<AiKuaishouAccountAutoVideo> pageList = aiKuaishouAccountAutoVideoService.list(queryWrapper);
+        //导出文件名称
+        mv.addObject(NormalExcelConstants.FILE_NAME, "自动投放账户——素材列表");
+        mv.addObject(NormalExcelConstants.CLASS, AiKuaishouAccountAutoVideo.class);
+        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("自动投放账户——素材列表数据", "导出人:Jeecg", "导出信息"));
+        mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
+        return mv;
+    }
+
+    /**
+     * 通过excel导入数据
+     *
+     * @param request
+     * @param response
+     * @return
+     */
+    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
+    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
+        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
+        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
+        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
+            MultipartFile file = entity.getValue();
+            ImportParams params = new ImportParams();
+            params.setTitleRows(2);
+            params.setHeadRows(1);
+            params.setNeedSave(true);
+            try {
+                List<AiKuaishouAccountAutoVideo> listAiKuaishouAccountAutoVideos = ExcelImportUtil.importExcel(file.getInputStream(), AiKuaishouAccountAutoVideo.class, params);
+                aiKuaishouAccountAutoVideoService.saveBatch(listAiKuaishouAccountAutoVideos);
+                return Result.ok("文件导入成功!数据行数:" + listAiKuaishouAccountAutoVideos.size());
+            } catch (Exception e) {
+                log.error(e.getMessage(), e);
+                return Result.error("文件导入失败:" + e.getMessage());
+            } finally {
+                try {
+                    file.getInputStream().close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+        return Result.ok("文件导入失败!");
+    }
+
+}

+ 101 - 0
jeecg-boot-module-system/src/main/java/cn/com/ctop/kuaishou/modules/ai/entity/AiKuaishouAccountAutoVideo.java

@@ -0,0 +1,101 @@
+package cn.com.ctop.kuaishou.modules.ai.entity;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import com.fasterxml.jackson.annotation.JsonFormat;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+import lombok.experimental.Accessors;
+import org.jeecgframework.poi.excel.annotation.Excel;
+import org.springframework.format.annotation.DateTimeFormat;
+
+import java.util.Date;
+
+/**
+ * 自动投放账户——素材
+ *
+ * @author jeecg-boot
+ * @version V1.0
+ * @date 2021-10-20
+ */
+@Data
+@TableName("ctop_ai_kuaishou_account_auto_video")
+@EqualsAndHashCode(callSuper = false)
+@Accessors(chain = true)
+@ApiModel(value = "ctop_ai_kuaishou_account_auto_video对象", description = "自动投放账户——素材")
+public class AiKuaishouAccountAutoVideo {
+
+    /**
+     * id
+     */
+    @TableId(type = IdType.AUTO)
+    @ApiModelProperty(value = "id")
+    private Long id;
+    /**
+     * 账户ID
+     */
+    @Excel(name = "账户ID", width = 15)
+    @ApiModelProperty(value = "账户ID")
+    private Long accountId;
+    /**
+     * 0 无效,1 有效
+     */
+    @Excel(name = "0 无效,1 有效", width = 15)
+    @ApiModelProperty(value = "0 无效,1 有效")
+    private Integer status;
+    /**
+     * 视频id
+     */
+    @Excel(name = "视频id", width = 15)
+    @ApiModelProperty(value = "视频id")
+    private String photoId;
+    /**
+     * 1 竖版视频 2 横版视频
+     */
+    @Excel(name = "1 竖版视频 2 横版视频", width = 15)
+    @ApiModelProperty(value = "1 竖版视频 2 横版视频")
+    private Integer materialType;
+    /**
+     * 0:自产 1:素造
+     */
+    @Excel(name = "0:自产 1:素造", width = 15)
+    @ApiModelProperty(value = "0:自产 1:素造")
+    private Integer channelType;
+    /**
+     * 使用时间
+     */
+    @Excel(name = "使用时间", width = 15)
+    @ApiModelProperty(value = "使用时间")
+    private String statDate;
+    /**
+     * MD5值
+     */
+    @Excel(name = "MD5值", width = 15)
+    @ApiModelProperty(value = "MD5值")
+    private String signature;
+    /**
+     * 素材名称
+     */
+    @Excel(name = "素材名称", width = 15)
+    @ApiModelProperty(value = "素材名称")
+    private String photoName;
+    /**
+     * 创建时间
+     */
+    @Excel(name = "创建时间", width = 20, format = "yyyy-MM-dd HH:mm:ss")
+    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
+    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
+    @ApiModelProperty(value = "创建时间")
+    private Date createTime;
+    /**
+     * 修改时间
+     */
+    @Excel(name = "修改时间", width = 20, format = "yyyy-MM-dd HH:mm:ss")
+    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
+    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
+    @ApiModelProperty(value = "修改时间")
+    private Date updateTime;
+}

+ 19 - 0
jeecg-boot-module-system/src/main/java/cn/com/ctop/kuaishou/modules/ai/mapper/AiKuaishouAccountAutoVideoMapper.java

@@ -0,0 +1,19 @@
+package cn.com.ctop.kuaishou.modules.ai.mapper;
+
+import cn.com.ctop.kuaishou.modules.ai.entity.AiKuaishouAccountAutoVideo;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.List;
+
+/**
+ * 自动投放账户——素材
+ *
+ * @author jeecg-boot
+ * 2021-10-20
+ * @version V1.0
+ */
+public interface AiKuaishouAccountAutoVideoMapper extends BaseMapper<AiKuaishouAccountAutoVideo> {
+
+    void insertBatch(@Param("videoGetList") List<AiKuaishouAccountAutoVideo> videoGetList);
+}

+ 31 - 0
jeecg-boot-module-system/src/main/java/cn/com/ctop/kuaishou/modules/ai/mapper/xml/AiKuaishouAccountAutoVideoMapper.xml

@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="cn.com.ctop.kuaishou.modules.ai.mapper.AiKuaishouAccountAutoVideoMapper">
+
+    <insert id="insertBatch">
+        replace into ctop_ai_kuaishou_account_auto_video(
+        account_id,
+        photo_id,
+        status,
+        material_type,
+        stat_date,
+        channel_type,
+        signature,
+        photo_name
+        )
+        values
+        <foreach collection="videoGetList" item="video" separator=",">
+            (
+            #{video.accountId},
+            #{video.photoId},
+            #{video.status},
+            #{video.materialType},
+            #{video.statDate},
+            #{video.channelType},
+            #{video.signature},
+            #{video.photoName}
+            )
+        </foreach>
+    </insert>
+
+</mapper>

+ 139 - 0
jeecg-boot-module-system/src/main/java/cn/com/ctop/kuaishou/modules/ai/service/impl/AiKuaishouAccountAutoVideoServiceImpl.java

@@ -0,0 +1,139 @@
+package cn.com.ctop.kuaishou.modules.ai.service.impl;
+
+import cn.com.ctop.common.module.entity.CtopOauthToken;
+import cn.com.ctop.common.module.enums.MaterialEnum;
+import cn.com.ctop.common.module.service.ICtopOauthTokenService;
+import cn.com.ctop.common.module.utils.Check;
+import cn.com.ctop.common.module.utils.HttpUtils;
+import cn.com.ctop.common.module.utils.KuaishouInterfaceConstant;
+import cn.com.ctop.common.module.utils.PropertiesUtils;
+import cn.com.ctop.kuaishou.modules.ai.entity.AiKuaishouAccountAutoVideo;
+import cn.com.ctop.kuaishou.modules.ai.mapper.AiKuaishouAccountAutoVideoMapper;
+import cn.com.ctop.kuaishou.modules.ai.service.IAiKuaishouAccountAutoVideoService;
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+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.util.DateUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * 自动投放账户——素材
+ *
+ * @author jeecg-boot
+ * 2021-10-20
+ * @version V1.0
+ */
+@Slf4j
+@Service
+public class AiKuaishouAccountAutoVideoServiceImpl extends ServiceImpl<AiKuaishouAccountAutoVideoMapper, AiKuaishouAccountAutoVideo> implements IAiKuaishouAccountAutoVideoService {
+
+    @Autowired
+    private AiKuaishouAccountAutoVideoMapper aiKuaishouAccountAutoVideoMapper;
+
+    @Autowired
+    private ICtopOauthTokenService tokenService;
+
+    @Override
+    public void insertAutoVideo(Long accountId) {
+        CtopOauthToken token = tokenService.getTokenByAccountId(accountId);
+        this.getVideoListByPage(token, 1);
+    }
+
+    private void getVideoListByPage(CtopOauthToken token, int page) {
+        try {
+            String date = DateUtils.formatDate(new Date());
+            Integer pageSize = 500;
+            String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.VIDEO_LIST;
+            Map<String, String> headers = new HashMap<>();
+            headers.put("Content-Type", "application/json");
+            headers.put("Access-Token", token.getAccessToken());
+            Map<String, Object> param = new HashMap<>();
+            param.put("advertiser_id", token.getAccountId());
+            param.put("page_size", pageSize);
+            param.put("page", page);
+            param.put("start_date", date);
+            param.put("end_date", date);
+            String result = HttpUtils.httpPostRequest(url, param, headers);
+            JSONObject resultJson = JSONObject.parseObject(result);
+            if (Check.isNull(resultJson)) {
+                return;
+            }
+            Integer code = resultJson.getInteger("code");
+            String message = resultJson.getString("message");
+            if (null == code || code != 0) {
+                log.error("获取快手视频列表数据异常:{},accountId:{}", message, token.getAccountId());
+                return;
+            }
+            JSONArray details = resultJson.getJSONObject("data").getJSONArray("details");
+            if (null == details || details.size() <= 0) {
+                log.info("快手视频列表信息为空=》accountId:{}", token.getAccountId());
+                return;
+            }
+            Boolean doGet = true;
+            if (details.size() < pageSize) {
+                doGet = false;
+            }
+
+            List<AiKuaishouAccountAutoVideo> videoGetList = new ArrayList<>();
+            for (int i = 0; i < details.size(); i++) {
+                JSONObject detailJson = details.getJSONObject(i);
+                if (Check.isNull(detailJson)) {
+                    continue;
+                }
+                String photoId = detailJson.getString("photo_id");
+                Long accountId = token.getAccountId();
+                QueryWrapper<AiKuaishouAccountAutoVideo> queryWrapper = new QueryWrapper<>();
+                queryWrapper.eq("photo_id", photoId);
+                queryWrapper.eq("account_id", accountId).last("limit 1");
+                AiKuaishouAccountAutoVideo one = this.getOne(queryWrapper);
+                if (!Check.isNull(one)) {
+                    continue;
+                }
+                AiKuaishouAccountAutoVideo videoGet = new AiKuaishouAccountAutoVideo();
+                videoGet.setAccountId(accountId);
+                videoGet.setPhotoId(photoId);
+                videoGet.setPhotoName(detailJson.getString("photo_name"));
+                videoGet.setStatDate(date);
+                String signature = detailJson.getString("signature");
+                videoGet.setSignature(signature);
+                Integer source = detailJson.getInteger("source");
+                if (source == 2) {
+                    videoGet.setChannelType(1);
+                } else {
+                    videoGet.setChannelType(0);
+                }
+                Integer newStatus = detailJson.getInteger("new_status");
+                if (newStatus == 1) {
+                    //状态1是可用,其他状态不录存
+                    videoGet.setStatus(1);
+                } else {
+                    continue;
+                }
+                Integer width = detailJson.getInteger("width");
+                Integer height = detailJson.getInteger("height");
+                Integer type = MaterialEnum.getTypeBySize(width, height);
+                if (!Check.isNull(type)) {
+                    videoGet.setMaterialType(type);
+                }
+                videoGetList.add(videoGet);
+            }
+            if (!Check.isNull(videoGetList)) {
+                aiKuaishouAccountAutoVideoMapper.insertBatch(videoGetList);
+            }
+            if (doGet) {
+                getVideoListByPage(token, page + 1);
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+}