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

import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.service.ICtopOauthTokenService;
import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.kuaishou.modules.ai.service.IKuaiShouBatchUpdateService;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouGroupService;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaishouInterfaceService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
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.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
@RestController
@RequestMapping("/ai/batchUpdate/")
public class KuaiShouBatchUpdateController {
    @Autowired
    private ICtopOauthTokenService tokenService;
    @Autowired
    private IKuaishouInterfaceService kuaishouInterfaceService;
    @Autowired
    private IKuaiShouBatchUpdateService batchUpdateService;
    @Autowired
    private IKuaiShouGroupService groupService;

    static ExecutorService executorService = Executors.newFixedThreadPool(5);

    @PostMapping(value = "/updateGroupBid")
    public JSONObject getAppDetail(@RequestBody JSONObject requestJson) {
        JSONObject returnJson = new JSONObject();
        try {
            Long accountId = requestJson.getLong("accountId");
            if (Check.isNull(accountId)) {
                throw new Exception("请输入账户");
            }
            CtopOauthToken oauthToken = tokenService.getTokenByAccountId(accountId);
            if (Check.isNull(oauthToken)) {
                throw new Exception("未获取到账户信息");
            }
            Long bid = requestJson.getLong("bid");
            if (Check.isNull(bid) || bid == 0) {
                throw new Exception("请传入需要修改的出价，或出价不能改为0");
            }
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        log.info("开始异步执行修改出价,accountId:{}", accountId);
                        groupService.deleteByAccountId(accountId);
                        log.info("历史数据清除完成,accountId:{}", accountId);
                        kuaishouInterfaceService.getGroupList(oauthToken, null, null);
                        log.info("最新数据获取完成,accountId:{}", accountId);
                        Thread.sleep(1000);
                        batchUpdateService.updateBid(oauthToken, bid);
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                    }
                }
            });


            returnJson.put("code", 0);
            returnJson.put("message", "success");

        } catch (Exception e) {
            returnJson.put("code", -1);
            returnJson.put("message", e.getMessage());
        }
        return returnJson;
    }

}
