package com.iformall.controller.rent;

import com.github.pagehelper.PageInfo;
import com.iformall.annotation.SystemControllerLog;
import com.iformall.common.Result;
import com.iformall.common.ResultData;
import com.iformall.controller.base.BaseController;
import com.iformall.domain.po.base.BaseEntity;
import com.iformall.domain.po.WxBillRent;
import com.iformall.service.WxBillRentService;
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.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author gongbiao
 */
@RestController
@RequestMapping("wxBillRent")
public class WxBillRentController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxBillRentService wxBillRentService;

    @GetMapping("list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)})
    @SystemControllerLog(description = "租赁押金账单-列表")
    public ResultData list(@ModelAttribute WxBillRent wxBillRent, Integer pageNum, Integer pageSize) {
        logger.debug("[" + getIpAddr() + "] WxBillRentController::list");
        if (null == wxBillRent) {
            wxBillRent = new WxBillRent();
        }
        wxBillRent.setTenantId(getTenantId());
        wxBillRent.setSortColumns(BaseEntity.SortField.Createtime_DESC,BaseEntity.SortField.Id_DESC);
        final PageInfo<WxBillRent> page = wxBillRentService.listAsPage(wxBillRent, pageNum, pageSize);
        return new ResultData(page);
    }

    @PostMapping("add")
    @SystemControllerLog(description = "租赁押金账单-新增")
    public ResultData add(@RequestBody WxBillRent wxBillRent) {
        logger.debug("[" + getIpAddr() + "] WxBillRentController::add");
        wxBillRent.setTenantId(getTenantId());
        return wxBillRentService.saveOrUpdate(wxBillRent, getUser());
    }

    @PostMapping("update")
    @SystemControllerLog(description = "租赁押金账单-更新")
    public ResultData update(@RequestBody WxBillRent wxBillRent) {
        logger.debug("[" + getIpAddr() + "] WxBillRentController::update");
        return wxBillRentService.saveOrUpdate(wxBillRent, getUser());
    }

    @ApiOperation("修改滞纳金")
    @PostMapping("updateLatePayMoney")
    @SystemControllerLog(description = "修改滞纳金")
    public ResultData updateLatePayMoney(@RequestBody WxBillRent wxBillRent) {
        logger.debug("[" + getIpAddr() + "] WxBillRentController::updateLatePayMoney");
        return wxBillRentService.updateLatePayMoney(wxBillRent, getUser());
    }

    @GetMapping("/del")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true)
    @SystemControllerLog(description = "租赁押金账单-删除")
    public ResultData delete(Long id) {
        logger.debug("[" + getIpAddr() + "] WxBillRentController::delete");
        wxBillRentService.deleteById(id);
        return new ResultData(Result.SUCCESS, "删除成功", null);
    }

    @GetMapping("/findById")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true)
    @SystemControllerLog(description = "租赁押金账单-id查找")
    public ResultData findById(Long id) {
        logger.debug("[" + getIpAddr() + "] WxBillRentController::findById");
        return new ResultData(Result.SUCCESS, "查询成功", wxBillRentService.getById(id));
    }

    @GetMapping("/findByRentContractId")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rentContractId", value = "merchantId", dataType = "Long", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)})
    @SystemControllerLog(description = "租赁押金账单-根据商户获取")
    public ResultData findByRentContractId(Long rentContractId, Integer pageNum, Integer pageSize) {
        logger.debug("[" + getIpAddr() + "] WxBillRentController::findByRentContractId");
        final PageInfo<WxBillRent> page = wxBillRentService.findByRentContractId(rentContractId, getTenantId(), pageNum, pageSize);
        return new ResultData(page);
    }

    @GetMapping("/updatePaid")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true)
    @SystemControllerLog(description = "租赁押金账单-支付更新")
    public ResultData updatePaid(Long id) {
        logger.debug("[" + getIpAddr() + "] WxBillRentController::updatePaid");
        return wxBillRentService.updatePaid(id);
    }

    @GetMapping("/detail")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true)
    @SystemControllerLog(description = "租赁押金账单-详情")
    public ResultData detail(Long id) {
        logger.debug("[" + getIpAddr() + "] WxBillRentController::detail");
        return new ResultData(Result.SUCCESS, "查询成功", wxBillRentService.detail(id));
    }

    @PostMapping("updateRevenue")
    @SystemControllerLog(description = "租赁押金账单-更新营业额")
    public ResultData updateRevenue(@RequestBody WxBillRent wxBillRent) {
        logger.debug("[" + getIpAddr() + "] WxBillRentController::updateRevenue");
        return wxBillRentService.updateRevenue(wxBillRent, getUser());
    }


    @PostMapping("updateLatePayStatus")
    @SystemControllerLog(description = "租赁押金账单-滞纳金结单")
    public ResultData updateLatePayStatus(@RequestBody WxBillRent wxBillRent) {
        logger.debug("[" + getIpAddr() + "] WxBillRentController::updateLatePayStatus");
        return wxBillRentService.updateLatePayStatus(wxBillRent);
    }

    @GetMapping("exportBill")
    @SystemControllerLog(description = "租赁押金账单-导出")
    public void exportBill(@ModelAttribute WxBillRent wxBillRent, HttpServletRequest request, HttpServletResponse response) {
        wxBillRent.setTenantId(getTenantId());
        wxBillRentService.exportBill(request, response, wxBillRent);
    }

}
