package com.ytpm.middle.controller;

import com.ytpm.general.Result;
import com.ytpm.general.ResultTable;
import com.ytpm.middle.param.AgentBaseInfoParam;
import com.ytpm.middle.param.AgentForm;
import com.ytpm.middle.service.AgentService;
import com.ytpm.middle.view.AgentBaseInfoListVO;
import com.ytpm.middle.view.MiddleUserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
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;

@Api(tags = "代理商管理")
@RestController
@RequestMapping("/agent")
public class AgentController {

    @Resource
    private AgentService agentService;

    /**
     * 代理商基本信息列表
     */
    @ApiOperation("代理商基本信息列表")
    @PostMapping("/baseInfoList")
    public ResultTable<AgentBaseInfoListVO> getBaseInfoList(@RequestBody AgentBaseInfoParam param){
        return agentService.getBaseInfoList(param);
    }

    /**
     * 新增代理商
     */
    @ApiOperation("新增代理商")
    @PostMapping("/addOne")
    public Result<String> addOne(@RequestBody AgentForm form, @ApiIgnore @AuthenticationPrincipal MiddleUserInfo userInfo){
        form.setCreateUserId(userInfo.getUserId());
        return agentService.addOne(form);
    }

    /**
     * 修改代理商
     */
    @ApiOperation("修改代理商")
    @PostMapping("/updateOne")
    public Result<String> updateOne(@RequestBody AgentForm form,  @ApiIgnore @AuthenticationPrincipal MiddleUserInfo userInfo){
        form.setUpdateParam(userInfo.getUserId());
        return agentService.updateOne(form);
    }

    /**
     * 启用或禁用
     */
    @ApiImplicitParam(name = "creditCode", value = "企业信用代码", required = true)
    @ApiOperation("启用或禁用")
    @GetMapping("/changeEnabled")
    public Result<String> enabled(@RequestParam(name = "creditCode")String creditCode,  @ApiIgnore @AuthenticationPrincipal MiddleUserInfo userInfo){
        return agentService.enabled(creditCode,userInfo.getUserId());
    }

    /**
     * 重置密码
     */
    @ApiImplicitParam(name = "creditCode", value = "企业信用代码", required = true)
    @ApiOperation("重置密码")
    @GetMapping("/resetPwd")
    public Result<String> resetPwd(@RequestParam(name = "creditCode")String creditCode){
        return agentService.resetPwd(creditCode);
    }
}
