package com.huiju.estateagents.property.controller;


import com.huiju.estateagents.base.BaseController;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.center.taUser.entity.TaUser;
import com.huiju.estateagents.property.common.UserElement;
import com.huiju.estateagents.property.model.Bill;
import com.huiju.estateagents.property.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.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 缴费项表 前端控制器
 * </p>
 *
 * @author weiximei
 * @since 2019-02-13
 */
@RestController
@RequestMapping("/api")
@Api(value = "缴费项 API", tags = "缴费项 API")
public class BillController extends BaseController {

    @Autowired
    private IBillService iBillService;
    
    
    // @ApiOperation(value = "根据搜索条件获取缴费项列表", notes = "根据搜索条件获取缴费项列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "billId", value = "收费组编号"),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "billName", value = "收费组名（模糊查询）"),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "billExplain", value = "收费组说明（模糊查询）"),
            @ApiImplicitParam(paramType = "query", dataType = "integer", name = "pageNum", value = "分页第几页"),
            @ApiImplicitParam(paramType = "query", dataType = "integer", name = "pageSize", value = "分页每页长度")
    })
    @RequestMapping(value = "/admin/bills",method = RequestMethod.GET)
    public ResponseBean getList(@RequestParam(value = "billId", required = false) Integer billId,
                                @RequestParam(value = "billName", required = false) String billName,
                                @RequestParam(value = "billExplain", required = false) String billExplain,
                                @RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        TaUser userElement = getTaUser(request);
        Bill bill = new Bill();
        bill.setId(billId);
        bill.setBillName(billName);
        bill.setBillExplain(billExplain);
        bill.setOrgId(userElement.getOrgId());
        responseBean = iBillService.getBillList(bill,pageNum,pageSize);
        return responseBean;
    }
    
    // @ApiOperation(value = "根据 id 查询缴费项详情", notes = "根据 id 查询缴费项详情")
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = String.class, paramType = "path", name = "id", value = "id编号")
    })
    @RequestMapping(value = "/admin/bill/{id}", method = RequestMethod.GET)
    public ResponseBean getByBillId(HttpServletRequest request, @PathVariable("id") Integer id) {
        ResponseBean responseBean = new ResponseBean();
        responseBean = iBillService.getByBillId(id);
        return responseBean;
    }

    // @ApiOperation(value = "上传缴费项接口, 预览", notes = "上传缴费项接口, 预览")
    @PostMapping(value = "/admin/bill/uploadExcel/get", consumes = "multipart/*", headers = "content-type=multipart/form-data")
    public ResponseBean getUploadExcel(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        ResponseBean responseBean = new ResponseBean();
        TaUser userElement = getTaUser(request);
        responseBean = iBillService.getExcelData(file, userElement);
        return responseBean;
    }

    // @ApiOperation(value = "上传缴费项接口, 保存/修改", notes = "上传缴费项接口, 保存/修改")
    @PostMapping(value = "/admin/bill/uploadExcel/add", consumes = "multipart/*", headers = "content-type=multipart/form-data")
    public ResponseBean addUploadExcel(HttpServletRequest request,
                                       @RequestParam(value = "billId", required = false) Integer billId,
                                       @RequestParam(value = "file", required = false) MultipartFile file,
                                       @RequestParam("billExplain") String billExplain,
                                       @RequestParam("billName") String billName,
                                       @RequestParam("endDate") Date endDate,
                                       @RequestParam("billStatus") String billStatus) {
        ResponseBean responseBean = new ResponseBean();
        TaUser userElement = getTaUser(request);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        Date date = null;
//        try {
//            String d = simpleDateFormat.format(new Long(endDate));
//            date = simpleDateFormat.parse(d);
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
        responseBean = iBillService.getExcelData(billId, file, billExplain, billName, endDate, billStatus, userElement);
        return responseBean;
    }


    // @ApiOperation(value = "根据 id 修改缴费项状态", notes = "根据 id 修改缴费项状态")
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = String.class, paramType = "path", name = "id", value = "id编号"),
            @ApiImplicitParam(dataTypeClass = String.class, paramType = "query", name = "billStatus", value = "状态 0：正在收费  2： 草稿")
    })
    @RequestMapping(value = "/admin/bill/updateBillStatus/{id}", method = RequestMethod.PUT)
    public ResponseBean updateBillStatus(HttpServletRequest request,
                                         @RequestParam("billStatus") Integer billStatus,
                                         @PathVariable("id") Integer id) {

        ResponseBean responseBean = new ResponseBean();
        TaUser userElement = getTaUser(request);
        responseBean = iBillService.updateBillStatus(userElement, billStatus, id);
        return responseBean;
    }

    // @ApiOperation(value = "根据 id集合 删除缴费项", notes = "根据 id集合 删除缴费项")
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = String.class, paramType = "body", name = "ids", value = "id编号集合"),

    })
    @RequestMapping(value = "/admin/bill/deleteBillBeach", method = RequestMethod.POST)
    public ResponseBean deleteBillBeach(HttpServletRequest request,
                                        @RequestBody List<Integer> ids) {

        ResponseBean responseBean = new ResponseBean();
        TaUser userElement = getTaUser(request);
        responseBean = iBillService.deleteBillBeach(userElement, ids);
        return responseBean;
    }

    // @ApiOperation(value = "下载Excel模板", notes = "下载Excel模板")
    @ApiImplicitParams({
//            @ApiImplicitParam(dataTypeClass = String.class, paramType = "body", name = "parameter", value = "billExplain收费组说明"),

    })
    @RequestMapping(value = "/admin/bill/downloadExcel", method = RequestMethod.GET)
    public void exportExcel(HttpServletResponse response, HttpServletRequest request) {
        TaUser userElement = getTaUser(request);
        HSSFWorkbook workbook = (HSSFWorkbook) iBillService.downloadExcel(userElement).getData();
//        this.setResponseHeader(response, "缴费单模板.xls");
        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=缴费单模板.xls");
        //响应到客户端
        try {
            OutputStream os = response.getOutputStream();
            workbook.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "获取本人的收费账单信息(已缴费或未缴费)", notes = "获取本人的收费账单信息(已缴费或未缴费)")
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "payType", value = "0是未缴费1是已缴费"),
            @ApiImplicitParam(paramType = "query", dataType = "integer", name = "pageNum", value = "分页第几页"),
            @ApiImplicitParam(paramType = "query", dataType = "integer", name = "pageSize", value = "分页每页长度"),
    })
    @RequestMapping(value = "/wx/bills/current_user/{payType}",method = RequestMethod.GET)
    public ResponseBean getBillInvoice(@PathVariable Integer payType,
                                       @RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                       @RequestParam(value ="pageSize",defaultValue = "5") Integer pageSize, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();

        Map<String, Object> map = iBillService.getBillsList(getPersonId(request), payType, pageNum, pageSize);
        responseBean.addSuccess(map);
        return responseBean;
    }

    @ApiOperation(value = "获取账单的详细信息", notes = "获取账单的详细信息")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "path", dataType = "String", name = "orgId", value = "小区Id"),
            @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "billInvoiceId", value = "账单id")})
    @RequestMapping(value = "/wx/bill/{orgId}/{billInvoiceId}",method = RequestMethod.GET)
    public ResponseBean getBillInvoiceDetail(@PathVariable Integer orgId,@PathVariable Integer billInvoiceId){
        ResponseBean responseBean = new ResponseBean();

        Map<String,Object> billInvoiceMap = iBillService.getBillInvoiceDetail(orgId,billInvoiceId);
        responseBean.addSuccess(billInvoiceMap);
        return responseBean;
    }

    @ApiOperation(value = "获取历史缴费", notes = "获取历史缴费")
    @ApiImplicitParams({

    })
    @RequestMapping(value = "/wx/bill/pay/history", method = RequestMethod.GET)
    public ResponseBean payHistory(HttpServletRequest request) {
        ResponseBean responseBean = new ResponseBean();
        TaUser userElement = getTaUser(request);
        responseBean = iBillService.payHistory(userElement);
        return responseBean;
    }

    @ApiOperation(value = "获取订单详情", notes = "获取订单详情")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataTypeClass = String.class, name = "orderNumber", value = "订单号")
    })
    @RequestMapping(value = "/wx/bill/order/info/{orderNumber}", method = RequestMethod.GET)
    public ResponseBean getOrderInfo(HttpSession session, @PathVariable("orderNumber") String orderNumber) {
        ResponseBean responseBean = new ResponseBean();
        responseBean = iBillService.getOrderInfo(orderNumber);
        return responseBean;
    }
}
