package com.hwrj.cloud.admin.controller;



import com.hwrj.cloud.admin.excelEntity.UmsRollBuyParam;
import com.hwrj.cloud.admin.model.UmsRollBuy;
import com.hwrj.cloud.admin.service.UmsRollBuyService;
import com.hwrj.cloud.admin.util.EasyExcelUtil;
import com.hwrj.cloud.common.api.CommonPage;
import com.hwrj.cloud.common.api.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * 走马灯Controller
 */
@Controller
@Api(tags = "UmsRollBuyController", description = "走马灯")
@RequestMapping("/rollbuy")
public class UmsRollBuyController {
    @Autowired
    private UmsRollBuyService umsRollBuyService;




    @ApiOperation("添加")
    @PostMapping("/add")
    @ResponseBody
    public CommonResult add(@RequestBody UmsRollBuy subject){
        int i = umsRollBuyService.add(subject);
        if (i==0){
           return CommonResult.failed();
        }
        return CommonResult.success();
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    @ResponseBody
    public CommonResult update(@RequestBody UmsRollBuy subject){
        int i = umsRollBuyService.update(subject);
        if (i==0){
            return CommonResult.failed();
        }
        return CommonResult.success();
    }


    @ApiOperation("删除")
    @GetMapping("/del")
    @ResponseBody
    public CommonResult delete(@RequestParam(value = "id")Long id){
        int i = umsRollBuyService.delete(id);
        if (i==0){
            return CommonResult.failed();
        }
        return CommonResult.success();
    }

    @ApiOperation("详细信息")
    @GetMapping("/get")
    @ResponseBody
    public CommonResult<UmsRollBuy> get(@RequestParam(value = "id")Long id){
        UmsRollBuy i = umsRollBuyService.info(id);
        if (i==null){
            return CommonResult.failed();
        }
        return CommonResult.success(i);
    }

    @ApiOperation("分页获取")
    @GetMapping("/page")
    @ResponseBody
    public CommonResult<CommonPage<UmsRollBuy>> get(@RequestParam(value = "keyword",required = false)String keyword
            ,@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum
            ,@RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){
        List<UmsRollBuy> categories = umsRollBuyService.page(keyword, pageNum, pageSize);

        return CommonResult.success(CommonPage.restPage(categories));
    }

    /**
     * 导入excel
     * @param files
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "导入")
    @RequestMapping(value = "excelImport", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult excelImport( @RequestParam("uploadFile") MultipartFile[] files,@RequestParam(value = "type",required = false)Integer type) throws Exception {
        List<UmsRollBuy> listAll = new ArrayList<>();
        if(files != null && files.length > 0){
            MultipartFile file = files[0];
            List<Object> list = EasyExcelUtil.readExcel(file, new UmsRollBuyParam(),1,2);
            if(list != null && list.size() > 0){
                for(Object o : list){
                    UmsRollBuyParam umsRollBuyParam = (UmsRollBuyParam) o;
                    UmsRollBuy umsRollBuy = new UmsRollBuy();
                    BeanUtils.copyProperties(umsRollBuyParam,umsRollBuy);
                    listAll.add(umsRollBuy);
                }
            }
        }
        int i = umsRollBuyService.importExcel(listAll);
        if (i<1){
            return CommonResult.failed();
        }

        return CommonResult.success();
    }
    @ApiOperation("导出模板")
    @PostMapping(value = "/out")
    public void out(HttpServletResponse response) throws IOException {

        //定义文件名和文件地址
        String downloadUrl = "excel/model/zoumaModel.xlsx";
        Resource resource = null;
        try {
            //获取资源文件
            resource = new ClassPathResource(downloadUrl);
        } catch (Exception e) {
            //自定义业务异常
            throw new RuntimeException("导出模板失败!");
        }
        byte[] buff = new byte[1024];
        BufferedInputStream bis = null;
        OutputStream os = null;
        try {
            // 配置文件下载
            response.setHeader("content-type", "application/octet-stream");
            response.setContentType("application/octet-stream");
            // 下载文件能正常显示中文
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("商品导入模板.xlsx", "UTF-8"));

            os = response.getOutputStream();
            bis = new BufferedInputStream(resource.getInputStream());
            int i = bis.read(buff);
            while (i != -1) {
                os.write(buff, 0, buff.length);
                os.flush();
                i = bis.read(buff);
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("文件下载出错!");
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }
}
