package com.community.huiju.controller;


import com.alibaba.fastjson.JSONObject;
import com.community.commom.mode.ResponseBean;
import com.community.commom.session.UserElement;
import com.community.commom.utils.PayPriceUtils;
import com.community.huiju.common.base.BaseController;
import com.community.huiju.model.BillInvoice;
import com.community.huiju.service.IBillInvoiceService;
import com.community.huiju.service.IBillService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * <p>
 * 缴费单 前端控制器
 * </p>
 *
 * @author weiximei
 * @since 2019-02-13
 */
@RestController
@RequestMapping("/")
@RefreshScope
@Api(value = "缴费单 API", description = "缴费单 API")
public class BillInvoiceController extends BaseController {

    @Autowired
    private IBillInvoiceService iBillInvoiceService;

    @Autowired
    private IBillService iBillService;

    @ApiOperation(value = "查询缴费单", notes = "查询缴费单")
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = String.class, paramType = "body", name = "parameter", value = "billId: 缴费项目id," +
                    "phase: 期,building: 栋,unit: 单元,roomNo: 户号,ownerName: 业主姓名," +
                    "billStatus: 缴费状态,payName: 缴费人,pageSize: 一页多少行,pageNum: 第几页"),
            @ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token"),
    })
    @RequestMapping(value = "/bill/invoice/list", method = RequestMethod.POST)
    public ResponseBean getBillList(HttpSession session, @RequestBody String parameter) {
        ResponseBean responseBean = new ResponseBean();
        UserElement userElement = getUserElement(session);
        responseBean = iBillInvoiceService.getBillList(parameter, userElement);
        return responseBean;
    }

    @ApiOperation(value = "修改缴费项 收费项名称,缴费项说明,截止时间", notes = "修改缴费项 收费项名称,缴费项说明,截止时间")
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = String.class, paramType = "body", name = "parameter", value = "billName收费项名称,billExplain缴费项说明,endDate截止时间"),
            @ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token")
    })
    @RequestMapping(value = "/bill/invoice/updateBill", method = RequestMethod.PUT)
    public ResponseBean updateBill(HttpSession session, @RequestBody String parameter) {
        ResponseBean responseBean = new ResponseBean();

        UserElement userElement = getUserElement(session);
        responseBean = iBillService.updateBillNameAndBillExplainAndEndDate(userElement, parameter);
        return responseBean;
    }

    @ApiOperation(value = "根据 id集合 删除缴费单", notes = "根据 id集合 删除缴费单")
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = String.class, paramType = "body", name = "ids", value = "id集合"),
            @ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token")
    })
    @RequestMapping(value = "/bill/invoice/delete", method = RequestMethod.POST)
    public ResponseBean delete(HttpSession session, @RequestBody List<Integer> ids) {
        ResponseBean responseBean = new ResponseBean();
        UserElement userElement = getUserElement(session);

        responseBean = iBillInvoiceService.deleteBillInvoice(ids, userElement);
        return responseBean;
    }

    @ApiOperation(value = "设置线下缴费", notes = "设置线下缴费")
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = String.class, paramType = "body", name = "ids", value = "id集合"),
            @ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token")
    })
    @RequestMapping(value = "/bill/invoice/offlinePayment", method = RequestMethod.POST)
    public ResponseBean offlinePayment(HttpSession session, @RequestBody List<Integer> ids) {
        ResponseBean responseBean = new ResponseBean();
        UserElement userElement = getUserElement(session);

        responseBean = iBillInvoiceService.offlinePayment(ids, userElement);
        return responseBean;
    }

    @ApiOperation(value = "由前台批量添加收费单, 默认定为 作废", notes = "由前台批量添加收费单, 默认定为 作废")
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = String.class, paramType = "body", name = "parameter", value = "billId缴费项编号,phase期/区," +
                    "building栋,unit单元,roomNo户号,worthOf截止时间(催缴日期),billInvoiceExplain收费单说明,payPrice收费金额"),
            @ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token")
    })
    @RequestMapping(value = "/bill/invoice/addTempBillInvoice", method = RequestMethod.POST)
    public ResponseBean addTempBillInvoice(HttpSession session, @RequestBody String parameter) {
        ResponseBean responseBean = new ResponseBean();
        UserElement userElement = getUserElement(session);
        responseBean = iBillInvoiceService.addTempBillInvoice(parameter, userElement);
        return responseBean;
    }

    @ApiOperation(value = "由前台批量添加收费单, 进行统一的提交, 修改为有效", notes = "由前台批量添加收费单, 进行统一的提交, 修改为有效")
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = String.class, paramType = "body", name = "ids", value = "id集合"),
            @ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token")
    })
    @RequestMapping(value = "/bill/invoice/updateBillInvoiceStatus", method = RequestMethod.POST)
    public ResponseBean updateBillInvoiceStatus(HttpSession session, @RequestBody List<Integer> ids) {
        ResponseBean responseBean = new ResponseBean();
        UserElement userElement = getUserElement(session);
        responseBean = iBillInvoiceService.updateBillInvoiceStatus(ids, userElement);
        return responseBean;
    }

    @ApiOperation(value = "查询前台批量添加的收费单, 状态是作废", notes = "查询前台批量添加的收费单, 状态是作废")
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = String.class, paramType = "path", name = "billId", value = "缴费id"),
            @ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token")
    })
    @RequestMapping(value = "/bill/invoice/getTempBillInvoice/{billId}", method = RequestMethod.GET)
    public ResponseBean getTempBillInvoice(HttpSession session, @PathVariable("billId") Integer billId) {
        ResponseBean responseBean = new ResponseBean();
        UserElement userElement = getUserElement(session);
        responseBean = iBillInvoiceService.getTempBillInvoice(billId, userElement);
        return responseBean;
    }

    @ApiOperation(value = "根据id删除收费单, 只能删除作废的", notes = "根据id删除收费单, 只能删除作废的")
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = String.class, paramType = "path", name = "billId", value = "缴费id"),
            @ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token")
    })
    @RequestMapping(value = "/bill/invoice/deleteTempBillInvoice/{billId}", method = RequestMethod.DELETE)
    public ResponseBean deleteTempBillInvoice(HttpSession session, @PathVariable("billId") Integer billId) {
        ResponseBean responseBean = new ResponseBean();
        UserElement userElement = getUserElement(session);
        responseBean = iBillInvoiceService.deleteTempBillInvoice(billId, userElement);
        return responseBean;
    }

    @ApiOperation(value = "根据缴费单id, 修改金额", notes = "根据缴费单id, 修改金额")
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = String.class, paramType = "path", name = "billId", value = "缴费id"),
            @ApiImplicitParam(dataTypeClass = String.class, paramType = "body", name = "parameter", value = "payPrice金额"),
            @ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token")
    })
    @RequestMapping(value = "/bill/invoice/updateBillInvoiceIdPayPrice/{billId}", method = RequestMethod.POST)
    public ResponseBean updateBillInvoiceIdPayPrice(HttpSession session,@PathVariable Integer billId, @RequestBody String parameter) {
        ResponseBean responseBean = new ResponseBean();
        UserElement userElement = getUserElement(session);
        Double payPrice = JSONObject.parseObject(parameter).getDouble("payPrice");
        responseBean = iBillInvoiceService.updateBillInvoiceIdPayPrice(billId, PayPriceUtils.yuanConversionPoints(payPrice) , userElement);
        return responseBean;
    }

    @ApiOperation(value = "根据缴费单id, 修改收费说明", notes = "根据缴费单id, 修改收费说明")
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = String.class, paramType = "path", name = "billId", value = "缴费id"),
            @ApiImplicitParam(dataTypeClass = String.class, paramType = "body", name = "parameter", value = "billInvoiceExplain收费说明"),
            @ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token")
    })
    @RequestMapping(value = "/bill/invoice/updateBillInvoiceIdBillInvoiceExplain/{billId}", method = RequestMethod.POST)
    public ResponseBean updateBillInvoiceIdBillInvoiceExplain(HttpSession session,@PathVariable Integer billId, @RequestBody String parameter) {
        ResponseBean responseBean = new ResponseBean();
        UserElement userElement = getUserElement(session);
        String billInvoiceExplain = JSONObject.parseObject(parameter).getString("billInvoiceExplain");
        if (StringUtils.isBlank(billInvoiceExplain)) {
            responseBean.addError("收费说明不能为空!");
            return responseBean;
        }
        BillInvoice billInvoice = iBillInvoiceService.getById(billId);
        billInvoice.setBillInvoiceExplain(billInvoiceExplain);
        boolean b = iBillInvoiceService.updateById(billInvoice);
        if (b) {
            responseBean.addSuccess("操作成功!");
            return responseBean;
        }
        responseBean.addError("操作失败!");
        return responseBean;
    }


    @ApiOperation(value = "导出Excel", notes = "导出Excel")
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = String.class, paramType = "path", name = "billId", value = "缴费id"),
            @ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token")
    })
    @RequestMapping(value = "/bill/invoice/exportExcel/{billId}", method = RequestMethod.GET)
    public void exportExcel(HttpServletResponse response, HttpSession session, @PathVariable("billId") Integer billId) {
        ResponseBean responseBean = new ResponseBean();
        UserElement userElement = getUserElement(session);
        HSSFWorkbook workbook = (HSSFWorkbook) iBillInvoiceService.exportExcel(userElement, billId).getData();
        this.setResponseHeader(response, "缴费单.xls");
        //响应到客户端
        try {
             OutputStream os = response.getOutputStream();
             workbook.write(os);
             os.flush();
             os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    @ApiOperation(value = "根据缴费项id, 查询草稿", notes = "根据缴费项id, 查询草稿")
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = String.class, paramType = "path", name = "billId", value = "billId缴费项id,pageNum当前页,pageSize一页多少行"),
            @ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token")
    })
    @RequestMapping(value = "/bill/invoice/getInvoiceInvalid", method = RequestMethod.POST)
    public ResponseBean getInvoiceInvalid(HttpSession session,@RequestBody String parameter) {
        ResponseBean responseBean = new ResponseBean();
        UserElement userElement = getUserElement(session);
        responseBean = iBillInvoiceService.getInvoiceInvalid(userElement, parameter);
        return responseBean;
    }


}
