package cn.com.ctop.kuaishou.modules.batch.controller;

import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.mapper.CtopOauthTokenMapper;
import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouCreative;
import cn.com.ctop.kuaishou.modules.batch.mapper.KuaiShouCreativeMapper;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouUpdateService;
import cn.com.ctop.kuaishou.modules.batch.service.IMaterialRefuseService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/MaterialRefuse")
public class MaterialRefuseController {
    @Autowired
    private IMaterialRefuseService materialRefuseService;
    @Autowired
    private CtopOauthTokenMapper oauthTokenMapper;
    @Autowired
    private IKuaiShouUpdateService updateService;
    @Autowired
    private KuaiShouCreativeMapper creativeMapper;


    @PostMapping(value = "/getRefuseCreative")
    public Map<String, Object> getRefuseCreative(@RequestBody JSONObject jsonObject) {
        Map<String, Object> map = new HashMap<>();
        try {
            List<JSONObject> list = materialRefuseService.getRefuseCreative(jsonObject.getLong("accountId"));
            map.put("code", 0);
            map.put("data", list);
        } catch (Exception e) {
            map.put("code", -1);
            e.printStackTrace();
        }
        return map;
    }

    @PostMapping(value = "/updateCreative")
    public Map<String, Object> updateCreative(@RequestBody JSONObject jsonObject) {
        Map<String, Object> map = new HashMap<>();
        if (Check.isNull(jsonObject)) {
            map.put("code", -1);
            map.put("message", "入参为空");
            return map;
        }

        Long accountId = jsonObject.getLong("accountId");
        QueryWrapper<CtopOauthToken> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_id", accountId);
        queryWrapper.eq("media_id", 2);
        queryWrapper.orderByDesc("create_time");
        queryWrapper.last("limit 1");
        CtopOauthToken ctopOauthToken = oauthTokenMapper.selectOne(queryWrapper);
        if (Check.isNull(ctopOauthToken)) {
            map.put("code", -1);
            map.put("message", "未获取授权信息");
            return map;
        }

        Map<String, Object> creativeMap = updateService.updateCreative(ctopOauthToken.getAccessToken(), accountId, jsonObject);
        if (Check.isNullMap(creativeMap)) {

            map.put("code", -1);
            map.put("message", "修改失败，返回为空");
            return map;
        }

        if ((Integer) creativeMap.get("code") != 0) {
            map.put("code", -1);
            map.put("message", creativeMap.get("message"));
            return map;
        }

        QueryWrapper<KuaiShouCreative> creativeQueryWrapper = new QueryWrapper<>();
        creativeQueryWrapper.eq("account_id", accountId);
        creativeQueryWrapper.eq("creative_id", jsonObject.getString("creativeId"));
        creativeQueryWrapper.eq("unit_id", jsonObject.getString("unitId"));
        creativeQueryWrapper.eq("campaign_id", jsonObject.getString("campaignId"));
        int i = creativeMapper.delete(creativeQueryWrapper);
        if (i > 0) {
            map.put("code", 0);
            map.put("message", "重审成功");
            return map;
        }

        return null;
    }

}
