package com.iformall.controller;

import com.iformall.common.Result;
import com.iformall.common.ResultData;
import com.iformall.domain.po.WxBillRent;
import com.iformall.domain.po.WxRentContract;
import com.iformall.service.WxBillRentService;
import com.iformall.service.WxRentContractService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
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;
import java.util.Map;

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

    @Autowired
    private WxRentContractService wxRentContractService;
    @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)})
    public ResultData list(@ModelAttribute WxRentContract wxRentContract, Integer pageNum, Integer pageSize) {
        logger.debug("[" + getIpAddr() + "] WxRentContractController::list");
        if (null == wxRentContract) {
            wxRentContract = new WxRentContract();
        }
        wxRentContract.setTenantId(getTenantId());
        Map<String, Object> result = wxRentContractService.listAsPage(wxRentContract, pageNum, pageSize);
        return new ResultData(result);
    }

    @PostMapping("add")
    public ResultData add(@RequestBody WxRentContract wxRentContract) {
        logger.debug("[" + getIpAddr() + "] WxRentContractController::add");
        wxRentContract.setTenantId(getTenantId());
        return wxRentContractService.save(wxRentContract, getUser().getId(),getUser().getName());
    }

    @PostMapping("update")
    public ResultData update(@RequestBody WxRentContract wxRentContract) {
        logger.debug("[" + getIpAddr() + "] WxRentContractController::update");
        wxRentContract.setTenantId(getTenantId());
        return wxRentContractService.update(wxRentContract, getUser().getId(),getUser().getName());
    }

    @PostMapping("updateMoney")
    public ResultData updateMoney(@RequestBody WxRentContract wxRentContract) {
        logger.debug("[" + getIpAddr() + "] WxRentContractController::updateMoney");
        wxRentContract.setTenantId(getTenantId());
        return wxRentContractService.update(wxRentContract, getUser().getId(),getUser().getName());
    }

    @PostMapping("updateFile")
    public ResultData updateFile(@RequestBody WxRentContract wxRentContract) {
        logger.debug("[" + getIpAddr() + "] WxRentContractController::updateFile");
        wxRentContract.setTenantId(getTenantId());
        return wxRentContractService.updateFile(wxRentContract, getUser().getId(),getUser().getName());
    }

    @GetMapping("/del")
    @ApiImplicitParam(name = "id", value = "id", dataType = "String", paramType = "query", required = true)
    public ResultData delete(String id) {
        logger.debug("[" + getIpAddr() + "] WxRentContractController::delete");
        return wxRentContractService.deleteById(id);
    }

    @GetMapping("/findById")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true)
    public ResultData findById(Long id) {
        logger.debug("[" + getIpAddr() + "] WxRentContractController::findById");
        // 同步欠缴状态
        WxBillRent wxBillRent = new WxBillRent();
        wxBillRent.setTenantId(super.getTenantId());
        wxBillRentService.updateBillStatus(wxBillRent);
        return wxRentContractService.getById(id);
    }

    @RequestMapping("/download")
    public void download(HttpServletRequest request, HttpServletResponse response){
        logger.debug("[" + getIpAddr() + "] WxRentContractController::download");
        wxRentContractService.download(request,response,getTenantId());
    }

    @GetMapping("/getRentContractStatusInfo")
    public ResultData getRentContractStatusInfo(@ModelAttribute WxRentContract wxRentContract) {
        logger.debug("[" + getIpAddr() + "] WxRentContractController::getRentContractStatusInfo");
        wxRentContract.setTenantId(getTenantId());
        return new ResultData(Result.SUCCESS, "查询成功", wxRentContractService.getRentContractStatusInfo(wxRentContract));
    }

    @GetMapping("/endRentContract")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true),
            @ApiImplicitParam(name = "status", value = "status", dataType = "int", paramType = "query", required = true)})
    public ResultData endRentContract(Long id,Integer status) {
        logger.debug("[" + getIpAddr() + "] WxRentContractController::endRentContract");
        return wxRentContractService.endRentContract(id,status);
    }

    @PostMapping("updateMerchant")
    public ResultData updateMerchant(@RequestBody WxRentContract wxRentContract) {
        logger.debug("[" + getIpAddr() + "] WxRentContractController::updateMerchant");
        return wxRentContractService.updateMerchant(wxRentContract);
    }

    @GetMapping("getRentContractList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)})
    public ResultData getRentContractList(@ModelAttribute WxRentContract wxRentContract, Integer pageNum, Integer pageSize) {
        logger.debug("[" + getIpAddr() + "] WxRentContractController::getRentContractList");
        wxRentContract.setTenantId(getTenantId());
        return new ResultData(wxRentContractService.getRentContractList(wxRentContract, pageNum, pageSize));
    }

    @GetMapping("exportContract")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true)})
    public void exportContract(Long id,HttpServletRequest request, HttpServletResponse response){
        wxRentContractService.exportContract(request,response,getTenantId(),id);
    }

    @GetMapping("updateRentContractStatus")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true)
    public ResultData updateRentContractStatus(Long id) {
        logger.debug("[" + getIpAddr() + "] WxRentContractController::updateRentContractStatus");
        return wxRentContractService.updateRentContractStatus(id);
    }

}
