package com.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.ssm.core.bean.PageResponse;
import com.ssm.core.util.SessionHelp;
import com.ssm.manage.query.DebtListRequest;
import com.ssm.manage.response.DebtListResponse;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmDebtService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@Api(value="欠款controller",tags={"会员欠款相关接口"})
@RestController
@RequestMapping("/demay/ssm/debt")
@Slf4j
public class SsmDebtController {

    @Autowired
    private SsmDebtService ssmDebtService;

    @ApiOperation(value = "欠款单分页查询  门店端-欠款单-欠款单分页查询",httpMethod = "POST",response = PageResponse.class)
    @RequestMapping("/page")
    public PageResponse selectPage(@RequestBody DebtListRequest debtListRequest, HttpSession session){
        PageResponse<DebtListResponse> pageResponse = new PageResponse<>();
        PageInfo<DebtListResponse> pageInfo = new PageInfo<>();
        debtListRequest.setStoreId(SessionHelp.getStoreId(session));
        try {
            pageInfo = ssmDebtService.selectPage(debtListRequest);
        }catch (Exception e){
            log.error("SsmDebtController.selectPage",e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }

//    @ApiOperation(value = "欠款模板下载  门店端-疗程-欠款模板下载", httpMethod = "POST", response = GenericResponse.class)
//    @RequestMapping("/download")
//    public void download(HttpSession session, HttpServletResponse response) {
//
//        // 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
//        // {} 代表普通变量 {.} 代表是list的变量 {前缀.} 前缀可以区分不同的list
//        ExcelWriter excelWriter = null;
//        OutputStream out = null;
//        try {
//            String fileName = "debt";
//            response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
//            response.setContentType("application/vnd.ms-excel;charset=utf-8");
//            response.setHeader("Content-Disposition",
//                    "attachment;filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1") + ".xlsx");
//            out = response.getOutputStream();
//            //ClassPathResource classPathResource = new ClassPathResource("static/live_room_data3.xls");
//            //InputStream inputStream = classPathResource.getInputStream();
//            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("config/debt.xlsx");
//
//            List<DebtList> list = new ArrayList<>();
//            WriteCellStyle headWriteCellStyle = new WriteCellStyle();
//            // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
//            //EasyExcel.write(out, LiveRoomData.class).sheet("模板").doWrite(list);
//            excelWriter = EasyExcel.write(out).withTemplate(inputStream).build();
//            //FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
//            WriteSheet writeSheet = EasyExcel.writerSheet().build();
//            excelWriter.fill(list, writeSheet);
//            out.flush();
//        } catch (Exception e) {
//            log.error("SsmDebtController.download:",e);
//        } finally {
//            //destFile.delete();
//            // 别忘记关闭流
//            excelWriter.finish();
//        }
//    }
//
//    @ApiOperation(value = "欠款单批量导出  门店端-还款-欠款单批量导出", httpMethod = "POST", response = GenericResponse.class)
//    @RequestMapping("/export")
//    public void export(@RequestBody DebtListRequest debtListRequest, HttpSession session) {
//        // 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
//        // {} 代表普通变量 {.} 代表是list的变量 {前缀.} 前缀可以区分不同的list
//        ExcelWriter excelWriter = null;
//        OutputStream out = null;
//        debtListRequest.setStoreId(SessionHelp.getStoreId(session));
//        debtListRequest.setStoreName(SessionHelp.getStoreName(session));
//        DebtList debtList = new DebtList();
//        try {
//            String fileName = "debt";
//            HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
//            response.setContentType("application/vnd.ms-excel;charset=utf-8");
//            response.setHeader("Content-Disposition",
//                    "attachment;filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1") + ".xlsx");
//            out = response.getOutputStream();
//            //ClassPathResource classPathResource = new ClassPathResource("static/live_room_data3.xls");
//            //InputStream inputStream = classPathResource.getInputStream();
//            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("config/debt.xlsx");
//            BeanUtils.copyProperties(debtListRequest, debtList);
//            List<DebtList> list = ssmDebtService.select(debtList);
//            WriteCellStyle headWriteCellStyle = new WriteCellStyle();
//            // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
//            //EasyExcel.write(out, LiveRoomData.class).sheet("模板").doWrite(list);
//            excelWriter = EasyExcel.write(out).withTemplate(inputStream).build();
//            //FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
//            WriteSheet writeSheet = EasyExcel.writerSheet().build();
//            excelWriter.fill(list, writeSheet);
//            out.flush();
//        } catch (Exception e) {
//            log.error("SsmDebtController.export:",e);
//        } finally {
//            //destFile.delete();
//            // 别忘记关闭流
//            excelWriter.finish();
//        }
//    }
//
//
//    @ApiOperation(value = "欠款单批量导入  门店端-还款-欠款单批量导入", httpMethod = "POST", response = GenericResponse.class)
//    @RequestMapping("/import")
//    public GenericResponse debtImport(MultipartFile multipartFile, HttpSession session) {
//        GenericResponse genericResponse = new GenericResponse();
//        Long storeId = SessionHelp.getStoreId(session);
//        String storeName = SessionHelp.getStoreName(session);
//        String phone = SessionHelp.getLoginAccount(session);
//        List<DebtList> list = null;
//        try {
//            list = EasyExcelUtil.simpleWrite(multipartFile, CourseAccount.class);
//        } catch (SmsException e) {
//            log.error("SsmDebtController.debtImport:",e);
//            return genericResponse;
//        }
//
//        int count = ssmDebtService.insertBatch(list, storeId, storeName);
//        if (list.size() != count) {
//            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
//            return genericResponse;
//        }
//        genericResponse.setCodeAndMsg(ResponseCode.OK);
//        return genericResponse;
//
//    }

}
