package com.ytpm.controller;

import com.ytpm.agent.model.YtDitch;
import com.ytpm.agent.param.AgentDitchParam;
import com.ytpm.agent.param.DitchListParam;
import com.ytpm.agent.view.AgentDitchView;
import com.ytpm.agent.view.AgentUserInfo;
import com.ytpm.general.Result;
import com.ytpm.general.ResultTable;
import com.ytpm.service.AgentDitchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
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 springfox.documentation.annotations.ApiIgnore;

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

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

    @Resource
    private AgentDitchService agentDitchService;

    /**
     * 获取渠道类型列表
     */
    @ApiOperation("获取渠道类型列表")
    @PostMapping("/list")
    public ResultTable<AgentDitchView> list(@RequestBody DitchListParam param, @ApiIgnore @AuthenticationPrincipal AgentUserInfo userInfo) {
        param.setUserId(userInfo.getUserId());
        return agentDitchService.ditchList(param);
    }

    /**
     * 新增渠道类型
     */
    @ApiOperation("新增渠道类型")
    @PostMapping("/addOne")
    public Result<?> addOne(@RequestBody AgentDitchParam param, @ApiIgnore @AuthenticationPrincipal AgentUserInfo userInfo){
        YtDitch ytDitch = new YtDitch();
        ytDitch.setDitchName(param.getDitchName());
        ytDitch.setAppType(param.getDitchType());
        ytDitch.setCreateTime(new Date());
        ytDitch.setUserId(userInfo.getUserId());
        return agentDitchService.addOne(ytDitch);
    }

    /**
     * 修改渠道类型
     */
    @ApiOperation("修改渠道类型")
    @PostMapping("/updateOne")
    public Result<?> updateOne(@RequestBody AgentDitchParam param){
        YtDitch ytDitch = new YtDitch();
        ytDitch.setAppType(param.getDitchType());
        ytDitch.setDitchName(param.getDitchName());
        ytDitch.setDitchId(param.getDitchId());
        return agentDitchService.updateOne(ytDitch);
    }

    /**
     * 删除渠道类型
     */
    @ApiOperation("删除渠道类型")
    @GetMapping("/deleteOne")
    public Result<?> deleteOne(@RequestParam("ditchId")Long ditchId){
        return agentDitchService.deleteOne(ditchId);
    }

}
