package com.ytpm.lemonios.controller;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageInfo;
import com.ytpm.agent.model.YtApp;
import com.ytpm.agent.param.DitchListForIosParam;
import com.ytpm.agent.param.DitchListParam;
import com.ytpm.agent.view.AgentDitchView;
import com.ytpm.annotation.ApiEncrypt;
import com.ytpm.annotation.Internal;
import com.ytpm.general.RepMessage;
import com.ytpm.general.Result;
import com.ytpm.general.ResultTable;
import com.ytpm.lemonios.service.DitchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.formatter.qual.InvalidFormat;
import org.springframework.web.bind.annotation.DeleteMapping;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

@Api(tags = "渠道类型管理模块")
@RestController
@RequestMapping("/ditch")
public class DitchController {

    @Resource
    private DitchService agentDitchService;

    /**
     * 获取渠道类型列表
     */
    @ApiEncrypt(response = true)
    @ApiOperation("获取渠道类型列表")
    @PostMapping("/list")
    public Result<List<AgentDitchView>> list(@RequestBody DitchListForIosParam param) {
        DitchListParam ditchListParam = new DitchListParam();
        return Result.resultOk(RepMessage.QUERY_SUCCESS, agentDitchService.ditchList(ditchListParam));
    }

    /**
     * 获取渠道类型列表
     */
    @ApiEncrypt(response = true)
    @ApiOperation("获取渠道类型列表")
    @PostMapping("/getDitchList")
    public ResultTable<AgentDitchView> getDitchList(@RequestBody DitchListForIosParam param) {
        param.setAppId(param.getAppId());
        DitchListParam ditchListParam = new DitchListParam();
        ditchListParam.setAppId(param.getAppId());
        return ResultTable.resultTableOk(new PageInfo<>(agentDitchService.ditchList(ditchListParam)));
    }

    /**
     * 新增渠道
     */

    @ApiOperation("新增渠道")
    @PostMapping("/addDitch")
    public Result<?> add(@RequestBody YtApp app) {
        return Result.resultOk(RepMessage.SAVE_SUCCESS, agentDitchService.save(app));
    }

    @ApiOperation("删除渠道")
    @DeleteMapping("/delDitch")
    public Result<?> delDitch(@RequestParam(name = "appId") String appId) {
        if (StrUtil.isNotEmpty(appId)) {
            agentDitchService.deleteByAppId(appId);
        }
        return Result.resultOk(RepMessage.SAVE_SUCCESS);
    }
}
