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.WxBillProperty;
import com.iformall.service.WxBillPropertyService;
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("wxBillProperty")
public class WxBillPropertyController extends BaseController {
    @Autowired
    private WxBillPropertyService wxBillPropertyService;

    private Logger logger = LoggerFactory.getLogger(WxBillPropertyController.class);

    @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 WxBillProperty wxBillProperty, Integer pageNum, Integer pageSize) {
        logger.debug("[" + getIpAddr() + "] WxBillPropertyController::list");
        if (null == wxBillProperty) {
            wxBillProperty = new WxBillProperty();
        }
        wxBillProperty.setTenantId(getTenantId());
        wxBillProperty.setSortColumns(BaseEntity.SortField.Createtime_DESC,BaseEntity.SortField.Id_DESC);
        PageInfo<WxBillProperty> result = wxBillPropertyService.listAsPage(wxBillProperty, pageNum, pageSize);
        return new ResultData(result);
    }

    @PostMapping("add")
    @SystemControllerLog(description = "物业账单-新增")
    public ResultData add(@RequestBody WxBillProperty wxBillProperty) {
        logger.debug("[" + getIpAddr() + "] WxBillPropertyController::add");
        wxBillProperty.setTenantId(getTenantId());
        wxBillPropertyService.saveOrUpdate(wxBillProperty, getUser());
        return new ResultData();
    }

    @PostMapping("update")
    @SystemControllerLog(description = "物业账单-更新")
    public ResultData update(@RequestBody WxBillProperty wxBillProperty) {
        logger.debug("[" + getIpAddr() + "] WxBillPropertyController::update");
        return wxBillPropertyService.saveOrUpdate(wxBillProperty, getUser());
    }

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

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

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

    @GetMapping("/updatePaid")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true)
    @SystemControllerLog(description = "物业账单-支付更新")
    public ResultData updatePaid(Long id) {
        logger.debug("[" + getIpAddr() + "] WxBillPropertyController::updatePaid");
        return wxBillPropertyService.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() + "] WxBillPropertyController::detail");
        return new ResultData(Result.SUCCESS, "查询成功", wxBillPropertyService.detail(id));
    }

    @GetMapping("/findByPropertyContractId")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "propertyContractId", 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 findByPropertyContractId(Long propertyContractId, Integer pageNum, Integer pageSize) {
        logger.debug("[" + getIpAddr() + "] WxBillPropertyController::findByPropertyContractId");
        final PageInfo<WxBillProperty> page = wxBillPropertyService.findByPropertyContractId(propertyContractId, getTenantId(), pageNum, pageSize);
        return new ResultData(page);
    }


    @PostMapping("updateLatePayStatus")
    @SystemControllerLog(description = "物业账单-滞纳金结单")
    public ResultData updateLatePayStatus(@RequestBody WxBillProperty wxBillProperty) {
        logger.debug("[" + getIpAddr() + "] WxBillPropertyController::updateLatePayStatus");
        return wxBillPropertyService.updateLatePayStatus(wxBillProperty);
    }

    @GetMapping("exportBill")
    @SystemControllerLog(description = "物业账单-导出")
    public void exportBill(@ModelAttribute WxBillProperty wxBillProperty, HttpServletRequest request, HttpServletResponse response) {
        wxBillProperty.setTenantId(getTenantId());
        wxBillPropertyService.exportBill(request, response, wxBillProperty);
    }
}
