package cn.pluss.platform.controller;

import cn.pluss.platform.agent.AgentService;
import cn.pluss.platform.api.PageInfo;
import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.dto.UserInfoDTO;
import cn.pluss.platform.entity.MerchantRateNew;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.service.agent.AgentStaffService;
import cn.pluss.platform.util.PageUtils;
import cn.pluss.platform.vo.AgentInfo;
import cn.pluss.platform.vo.AgentStaffVO;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 超级服务商相关功能
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("agent")
public class AgentInfoController {

    @Setter(onMethod_ = {@Autowired})
    private AgentService agentService;
    @Setter(onMethod_ = {@Autowired})
    private AgentStaffService agentStaffService;


    @PostMapping("saveOrUpdateStaff")
    public Result<?> saveOrUpdateStaff(@RequestBody @Valid UserInfoDTO userInfoDTO) {
        agentStaffService.saveOrUpdateStaff(userInfoDTO);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("saveStaff")
    public Result<?> saveStaff(@RequestBody @Valid UserInfoDTO userInfoDTO) {
        agentStaffService.saveStaff(userInfoDTO);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("updateStaff")
    public Result<?> updateStaff(@RequestBody @Valid UserInfoDTO userInfoDTO) {
        agentStaffService.updateStaff(userInfoDTO);
        return ResultGenerator.genSuccessResult();
    }

    @RequestMapping(value = "delStaff/{userId}", method = {RequestMethod.DELETE, RequestMethod.GET})
    public Result<?> delStaff(@PathVariable String userId) {
        agentStaffService.delStaff(userId);
        return ResultGenerator.genSuccessResult("删除成功", null);
    }

    @GetMapping("staff/{userId}")
    public Result<AgentStaffVO> getStaff(@PathVariable String userId) {
        AgentStaffVO staff = agentStaffService.getStaff(userId);
        return ResultGenerator.genSuccessResult(staff);
    }

    @GetMapping("staffPage")
    public Result<PageInfo<AgentStaffVO>> staffPage(Page<AgentStaffVO> page,  UserInfoDTO userInfoDTO) {
        if (userInfoDTO.getPId() == null) {
            throw new MsgException("超级服务商id必传");
        }

        Page<AgentStaffVO> result = agentStaffService.staffPage(page, userInfoDTO);
        return ResultGenerator.genSuccessResult(PageUtils.page(result));
    }

    @GetMapping("page")
    public Result<Page<AgentInfo>> page(Page<AgentInfo> page, AgentInfo agentInfo, OrderItem orderItem) {
        page = agentService.page(page, agentInfo, orderItem);
        return ResultGenerator.genSuccessResult(page);
    }

    @PostMapping("save")
    public Result<String> save(@RequestBody @Valid AgentInfo agentInfo) {
        agentService.save(agentInfo);
        return ResultGenerator.genSuccessResult("保存成功", "保存成功");
    }

    /**
     * 修改商户费率的最小费率阈值
     * @return
     */
    @GetMapping("minSettingRateNew")
    public Result<MerchantRateNew> minSettingRateNew() {
        MerchantRateNew minSettingRateNew = agentService.getMinSettingRateNew();
        return ResultGenerator.genSuccessResult(minSettingRateNew);
    }
}
