package com.cyksj.web.controller.manage.mj;

import cn.hutool.core.util.StrUtil;
import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.model.entity.MidjourneyAccount;
import com.cyksj.model.manage.views.AccountView;
import com.cyksj.model.views.MidjourneyAccountView;
import com.cyksj.service.midjourney.MidjourneyAccountService;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.SearchResult;
import com.ejlchina.searcher.param.Operator;
import com.ejlchina.searcher.util.MapBuilder;
import com.ejlchina.searcher.util.MapUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zwhui
 * @date 2024/4/15 14:38
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/manage/mj")
public class MidJourneyController {

    private final BeanSearcher beanSearcher;

    private final HttpServletRequest request;

    private final MidjourneyAccountService midjourneyAccountService;


    /**
     * 移入MJ账号池
     */
    @PostMapping("/addAccount")
    public Result<Void> addAccount(@RequestBody MidjourneyAccount midjourneyAccount) throws Exception {
        log.info("移入MJ账号池 midjourneyAccount:{}", midjourneyAccount);
        midjourneyAccountService.addAccount(midjourneyAccount);
        return GatewayResponse.SUCCESS.newBuilder().toResult();
    }

    /**
     * 移除MJ账号池
     */
    @DeleteMapping("/removeAccount/{id}")
    public Result<Void> removeAccount(@PathVariable Long id) {
        log.info("移除MJ账号池 id:{}", id);
        midjourneyAccountService.removeAccount(id);
        return GatewayResponse.SUCCESS.newBuilder().toResult();
    }

    /**
     * 修改MJ账号池
     */
    @PutMapping("/updateAccount")
    public Result<Void> updateAccount(@RequestBody MidjourneyAccount midjourneyAccount) {
        log.info("修改MJ账号池 midjourneyAccount:{}", midjourneyAccount);
        midjourneyAccountService.updateAccount(midjourneyAccount);
        return GatewayResponse.SUCCESS.newBuilder().toResult();
    }

    /**
     * 上下架MJ账号
     */
    @PutMapping("/updateStatus/{id}")
    public Result<Void> updateStatus(@PathVariable Long id) {
        log.info("上下架MJ账号 id:{}", id);
        midjourneyAccountService.updateStatus(id);
        return GatewayResponse.SUCCESS.newBuilder().toResult();
    }

    /**
     * 同步账号信息
     */
    @PutMapping("/syncAccount/{id}")
    public Result<Void> syncAccount(@PathVariable Long id) {
        log.info("同步账号信息 id:{}", id);
        midjourneyAccountService.syncAccount(id);
        return GatewayResponse.SUCCESS.newBuilder().toResult();
    }


    /**
     * 查询账号池列表
     */
    @GetMapping("/getAccount")
    public Result<SearchResult<MidjourneyAccountView>> getAccount(String expiryStartTime, String expiryEndTime) {
        log.info("查询账号池列表");
        MapBuilder mapBuilder = MapUtils.flatBuilder(request.getParameterMap());
        if (StrUtil.isNotEmpty(expiryStartTime) || StrUtil.isNotEmpty(expiryEndTime)) {
            mapBuilder.field(MidjourneyAccountView::getExpiryTime, expiryStartTime, expiryEndTime)
                    .op(Operator.Between);
        }
        SearchResult<MidjourneyAccountView> search = beanSearcher.search(MidjourneyAccountView.class, mapBuilder.build());
        return GatewayResponse.SUCCESS.newBuilder().toResult(search);
    }



}
