package com.community.huiju.controller;


import com.community.commom.mode.ResponseBean;
import com.community.commom.session.UserElement;
import com.community.huiju.common.base.BaseController;
import com.community.huiju.model.Bill;
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.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 org.springframework.web.multipart.MultipartFile;

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;

/**
 * <p>
 * 缴费项表 前端控制器
 * </p>
 *
 * @author weiximei
 * @since 2019-02-13
 */
@RestController
@RequestMapping("/")
@RefreshScope
@Api(value = "缴费项 API", description = "缴费项 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 = "/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, HttpSession session){
        ResponseBean responseBean = new ResponseBean();
        UserElement userElement = getUserElement(session);
        Bill bill = new Bill();
        bill.setId(billId);
        bill.setBillName(billName);
        bill.setBillExplain(billExplain);
        bill.setCommunityId(userElement.getCommunityId());
        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 = "/bill/{id}", method = RequestMethod.GET)
    public ResponseBean getByBillId(HttpSession session, @PathVariable("id") Integer id) {
        ResponseBean responseBean = new ResponseBean();
        responseBean = iBillService.getByBillId(id);
        return responseBean;
    }

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

    @ApiOperation(value = "上传缴费项接口, 保存/修改", notes = "上传缴费项接口, 保存/修改")
    @PostMapping(value = "/bill/uploadExcel/add", consumes = "multipart/*", headers = "content-type=multipart/form-data")
    public ResponseBean addUploadExcel(HttpSession session,
                                       @RequestParam(value = "billId", required = false) Integer billId,
                                       @RequestParam(value = "file", required = false) MultipartFile file,
                                       @RequestParam("billExplain") String billExplain,
                                       @RequestParam("billName") String billName,
                                       @RequestParam("endDate") String endDate,
                                       @RequestParam("billStatus") String billStatus) {
        ResponseBean responseBean = new ResponseBean();
        UserElement userElement = getUserElement(session);
        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, date, 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 = "/bill/updateBillStatus/{id}", method = RequestMethod.PUT)
    public ResponseBean updateBillStatus(HttpSession session,
                                         @RequestParam("billStatus") Integer billStatus,
                                         @PathVariable("id") Integer id) {

        ResponseBean responseBean = new ResponseBean();
        UserElement userElement = getUserElement(session);
        responseBean = iBillService.updateBillStatus(userElement, billStatus, id);
        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/deleteBillBeach", method = RequestMethod.POST)
    public ResponseBean deleteBillBeach(HttpSession session,
                                         @RequestBody List<Integer> ids) {

        ResponseBean responseBean = new ResponseBean();
        UserElement userElement = getUserElement(session);
        responseBean = iBillService.deleteBillBeach(userElement, ids);
        return responseBean;
    }

    @ApiOperation(value = "下载Excel模板", notes = "下载Excel模板")
    @ApiImplicitParams({
//            @ApiImplicitParam(dataTypeClass = String.class, paramType = "body", name = "parameter", value = "billExplain收费组说明"),
            @ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token")
    })
    @RequestMapping(value = "/bill/downloadExcel", method = RequestMethod.GET)
    public void exportExcel(HttpServletResponse response, HttpSession session) {
        UserElement userElement = getUserElement(session);
        HSSFWorkbook workbook = (HSSFWorkbook) iBillService.downloadExcel(userElement).getData();
        this.setResponseHeader(response, "缴费单模板.xls");
        //响应到客户端
        try {
            OutputStream os = response.getOutputStream();
            workbook.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
