package com.iformall.controller;

import com.iformall.common.Result;
import com.iformall.common.ResultData;
import com.iformall.domain.po.WxBillProperty;
import com.iformall.domain.po.WxFlowRecord;
import com.iformall.domain.po.WxPropertyContract;
import com.iformall.domain.po.WxRentContract;
import com.iformall.enums.EnumFlowRecordStatus;
import com.iformall.service.WxBillPropertyService;
import com.iformall.service.WxPropertyContractService;
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;
import java.util.Map;

/**
 * @author gongbiao
 */
@RestController
@RequestMapping("wxPropertyContract")
public class WxPropertyContractController extends BaseController {
	@Autowired
    private WxPropertyContractService wxPropertyContractService;
	@Autowired
    private WxBillPropertyService wxBillPropertyService;

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

    @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 WxPropertyContract wxPropertyContract,Integer pageNum, Integer pageSize) {
        logger.debug("[" + getIpAddr() + "] WxPropertyContractController::list");
    	if (null == wxPropertyContract){
    	    wxPropertyContract = new WxPropertyContract();
        }
        wxPropertyContract.setTenantId(getTenantId());
        Map<String, Object> result = wxPropertyContractService.listAsPage(wxPropertyContract, pageNum, pageSize);
        return new ResultData(result);
    }

    @PostMapping("add")
    public ResultData add(@RequestBody WxPropertyContract wxPropertyContract) {
        logger.debug("[" + getIpAddr() + "] WxPropertyContractController::add");
        wxPropertyContract.setTenantId(getTenantId());
        return wxPropertyContractService.saveOrUpdate(wxPropertyContract, getUser().getId(),getUser().getName());
    }

    @PostMapping("update")
    public ResultData update(@RequestBody WxPropertyContract wxPropertyContract) {
        logger.debug("[" + getIpAddr() + "] WxPropertyContractController::update");
        wxPropertyContract.setTenantId(getTenantId());
        return wxPropertyContractService.saveOrUpdate(wxPropertyContract, getUser().getId(),getUser().getName());
    }

    @GetMapping("/del")
     @ApiImplicitParam(name="id",value="id",dataType="Long", paramType = "query",required=true)
    public ResultData delete(Long id) {
        logger.debug("[" + getIpAddr() + "] WxPropertyContractController::delete");
        wxPropertyContractService.deleteById(id);
        return new ResultData(Result.SUCCESS, "删除成功", null);
    }
    
     @GetMapping("/findById")
     @ApiImplicitParam(name="id",value="id",dataType="Long", paramType = "query",required=true)
    public ResultData findById(Long id) {
        logger.debug("[" + getIpAddr() + "] WxPropertyContractController::findById");
        WxBillProperty wxBillProperty = new WxBillProperty();
        wxBillProperty.setTenantId(super.getTenantId());
        wxBillPropertyService.updateBillStatus(wxBillProperty);
    	return wxPropertyContractService.getById(id);
    }

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

    @GetMapping("/getPropertyContractStatusInfo")
    public ResultData getPropertyContractStatusInfo(@ModelAttribute WxPropertyContract wxPropertyContract) {
        logger.debug("[" + getIpAddr() + "] WxPropertyContractController::getPropertyContractStatusInfo");
        wxPropertyContract.setTenantId(getTenantId());
        return new ResultData(Result.SUCCESS, "查询成功", wxPropertyContractService.getRentContractStatusInfo(wxPropertyContract));
    }

    @GetMapping("/endPropertyContract")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true)
    public ResultData endRentContract(Long id) {
        logger.debug("[" + getIpAddr() + "] WxPropertyContractController::endRentContract");
        return wxPropertyContractService.endRentContract(id);
    }

    @GetMapping("/getRentContractList")
    public ResultData getRentContractList(@ModelAttribute WxPropertyContract wxPropertyContract) {
        logger.debug("[" + getIpAddr() + "] WxPropertyContractController::getRentContractList");
        wxPropertyContract.setTenantId(getTenantId());
        return new ResultData(Result.SUCCESS, "查询成功", wxPropertyContractService.getRentContractList(wxPropertyContract));
    }

    @GetMapping("/endPropertyContractByRentContractId")
    @ApiImplicitParam(name = "rentContractId", value = "rentContractId", dataType = "Long", paramType = "query", required = true)
    public ResultData endPropertyContractByRentContractId(Long rentContractId) {
        logger.debug("[" + getIpAddr() + "] WxPropertyContractController::endPropertyContractByRentContractId");
        return wxPropertyContractService.endPropertyContractByRentContractId(rentContractId);
    }

    @GetMapping("/hasRentContract")
    @ApiImplicitParam(name = "rentContractId", value = "rentContractId", dataType = "Long", paramType = "query", required = true)
    public ResultData hasRentContract(Long rentContractId) {
        logger.debug("[" + getIpAddr() + "] WxPropertyContractController::hasRentContract");
        return wxPropertyContractService.hasRentContract(rentContractId);
    }

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

}
