package com.iformall.controller;

import com.github.pagehelper.PageInfo;
import com.iformall.common.ErrorCode;
import com.iformall.common.ResultData;
import com.iformall.domain.po.WxMerchant;
import com.iformall.domain.po.WxProfitSharingReceiver;
import com.iformall.service.WxMerchantService;
import com.iformall.service.WxProfitSharingReceiverService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("wxProfitSharingReceiver")
public class WxProfitSharingReceiverController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxProfitSharingReceiverService wxProfitSharingReceiverService;
    @Autowired
    private WxMerchantService wxMerchantService;

    @ApiOperation("分页列表接口")
    @GetMapping("list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)})
    public ResultData list(@ModelAttribute WxProfitSharingReceiver receiver, Integer pageNum, Integer pageSize) {
        logger.debug("[" + getIpAddr() + "] WxProfitSharingReceiverController::list");
        if (null == receiver) receiver = new WxProfitSharingReceiver();
        final PageInfo<WxProfitSharingReceiver> page = wxProfitSharingReceiverService.listAsPage(receiver, pageNum, pageSize);
        return new ResultData(page);
    }


    @ApiOperation("新增接口")
    @PostMapping("add")
    public ResultData add(@ModelAttribute WxProfitSharingReceiver receiver) {
        logger.debug("[" + getIpAddr() + "] WxProfitSharingReceiverController::add");

        if (receiver == null)
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL);
        if (receiver.getMerchantId() == null)
            return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
        if (receiver.getReceiverType() == null)
            return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
        if (receiver.getReceiverComments() == null)
            return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
        if (receiver.getReceiverAccount() == null)
            return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
        if (receiver.getTrueName() == null)
            return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);

        WxMerchant merchant = wxMerchantService.getById(receiver.getMerchantId());
        if (merchant == null)
            return new ResultData(ErrorCode.MERCHANT_INFO_NOT_FOUND);

        return wxProfitSharingReceiverService.addReceiver(merchant, receiver);
    }


    @ApiOperation("根据id删除接口")
    @GetMapping("del")
    public ResultData delete(@ModelAttribute WxProfitSharingReceiver receiver) {
        logger.debug("[" + getIpAddr() + "] WxProfitSharingReceiverController::delete");
        if (receiver == null)
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL);
        if (receiver.getMerchantId() == null)
            return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
        if (receiver.getReceiverType() == null)
            return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
        if (receiver.getReceiverAccount() == null)
            return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);

        WxMerchant merchant = wxMerchantService.getById(receiver.getMerchantId());
        if (merchant == null)
            return new ResultData(ErrorCode.MERCHANT_INFO_NOT_FOUND);

        return wxProfitSharingReceiverService.delReceiver(merchant);
    }


}
