package com.org.hktshop.controller.encapsulation;

import com.org.hktshop.base.BaseException;
import com.org.hktshop.base.BaseResult;
import com.org.hktshop.constant.CommonConstant;
import com.org.hktshop.dto.ExcelColumnDto;
import com.org.hktshop.dto.VerifyCellDto;
import com.org.hktshop.entity.Page;
import com.org.hktshop.entity.encapsulation.FcProduct;
import com.org.hktshop.service.encapsulation.FcProductService;
import com.org.hktshop.utils.HttpCode;
import com.org.hktshop.utils.ListUtils;
import com.org.hktshop.utils.excel.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
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.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/fcProduct")
@Api(tags = "产品接口")
@Slf4j
public class FcProductController {

    @Resource
    private FcProductService fcProductService;

    @PostMapping("/save")
    @ApiOperation(value = "新增产品", notes = "新增产品")
    public BaseResult insert(@RequestBody FcProduct fcProduct) {
        try {
            fcProductService.saveFcProduct(fcProduct);
            return BaseResult.success();
        } catch (Exception ex) {
            log.error("新增产品: error = {}", ex.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), HttpCode.INTERNAL_SERVER_ERROR.getMsg());
        }
    }

    @GetMapping("/deleteById")
    @ApiOperation(value = "删除产品", notes = "删除产品")
    public BaseResult deleteById(@RequestParam String id) {
        try {
            fcProductService.deleteFcProduct(id);
            return BaseResult.success();
        } catch (Exception ex) {
            log.error("删除产品: error = {}", ex.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), HttpCode.INTERNAL_SERVER_ERROR.getMsg());
        }
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改产品", notes = "修改产品")
    public BaseResult update(@RequestBody FcProduct fcProduct) {
        try {
            fcProductService.updateFcProduct(fcProduct);
            return BaseResult.success();
        } catch (Exception ex) {
            log.error("修改产品: error = {}", ex.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), HttpCode.INTERNAL_SERVER_ERROR.getMsg());
        }
    }

    @GetMapping("/getById")
    @ApiOperation(value = "产品 id查", notes = "产品 id查")
    public BaseResult<FcProduct> getById(@RequestParam String id) {
        try {
            return BaseResult.successWithData(fcProductService.getFcProduct(id));
        } catch (Exception ex) {
            log.error("产品 id查: error = {}", ex.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), HttpCode.INTERNAL_SERVER_ERROR.getMsg());
        }
    }

    @PostMapping("/findPage")
    @ApiOperation(value = "产品 分页查", notes = "产品 分页查")
    public BaseResult<Page<FcProduct>> findPage(@RequestBody FcProduct fcProduct) {
        try {
            return BaseResult.successWithData(fcProductService.getFcProductPage(fcProduct));
        } catch (Exception ex) {
            log.error("产品 分页查: error = {}", ex.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), HttpCode.INTERNAL_SERVER_ERROR.getMsg());
        }
    }

//    @PostMapping("/findUserPage")
//    @ApiOperation(value = "产品 分页查", notes = "产品 分页查")
//    public BaseResult<Page<FcProduct>> findUserPage(@RequestBody FcProduct fcProduct) {
//        try {
//            return BaseResult.successWithData(fcProductService.getFcProductUserPage(fcProduct));
//        } catch (Exception ex) {
//            log.error("产品 分页查: error = {}", ex.toString());
//            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), HttpCode.INTERNAL_SERVER_ERROR.getMsg());
//        }
//    }

    @GetMapping("/importTemplate")
    @ApiOperation(value = "导出封装商品模板", notes = "导出封装商品模板")
    public void importTemplate(HttpServletResponse response) {
        try {
            String[] importTemplate = CommonConstant.IMPORT_TEMPLATE;
            HSSFWorkbook workbook = ExcelUtil.writeWorkBook(importTemplate, new ArrayList<>());
            ExcelUtil.ExportExcel(workbook, response);
        } catch (Exception ex) {
            log.error("导出封装商品模板: error = {}", ex.toString());
        }
    }

    @PostMapping("/import")
    @ApiOperation(value = "导入封装商品", notes = "导入封装商品")
    public BaseResult importFcProduct(@RequestParam(value = "excelFile") MultipartFile excelFile) {
        try {
            VerifyCellDto verifyCellDto = fcProductService.verifyImportData(excelFile);
            List<List<ExcelColumnDto>> rowList = verifyCellDto.getRowList();
            List dataset = fcProductService.assembleData(rowList);
            List<FcProduct> products = ListUtils.copyList(dataset, FcProduct.class);
            fcProductService.importData(products);
            return BaseResult.success();
        } catch (BaseException ex) {
            log.error("导入错误: error = {}", ex.toString());
            return BaseResult.fail(10002, ex.getMessage());
        } catch (Exception ex) {
            log.error("导入错误: error = {}", ex.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), HttpCode.INTERNAL_SERVER_ERROR.getMsg());
        }
    }

    @PostMapping("/verify")
    @ApiOperation(value = "校验封装商品", notes = "导出封装商品")
    public BaseResult verify(@RequestParam(value = "excelFile") MultipartFile excelFile) {
        try {
            VerifyCellDto verifyCellDto = fcProductService.verifyImportData(excelFile);
            if (verifyCellDto.isError()) {
                return BaseResult.fail(10002, "导入数据有错误");
            }
            return BaseResult.success();
        } catch (BaseException ex) {
            log.error("导入校验: error = {}", ex.toString());
            return BaseResult.fail(10003, ex.getMessage());
        } catch (Exception ex) {
            log.error("导入校验: error = {}", ex.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), HttpCode.INTERNAL_SERVER_ERROR.getMsg());
        }
    }

    @PostMapping("/downloadErrorExcel")
    @ApiOperation(value = "下载错误信息", notes = "下载错误信息")
    public void downloadErrorExcel(@RequestParam(value = "excelFile") MultipartFile excelFile, HttpServletResponse response) {
        try {
            VerifyCellDto verifyCellDto = fcProductService.verifyImportData(excelFile);
            String[] importTemplate = CommonConstant.IMPORT_TEMPLATE;
            if (verifyCellDto.isError()) {
                List<List<ExcelColumnDto>> rowList = verifyCellDto.getRowList();
                List dataset = fcProductService.assembleData(rowList);
                HSSFWorkbook workbook1 = ExcelUtil.writeWorkBook(importTemplate, dataset);
                ExcelUtil.ExportExcel(workbook1, response);
            }
        } catch (Exception ex) {
            log.error("下载错误信息: error = {}", ex.toString());
        }
    }

    @GetMapping("/getBrandNames")
    @ApiOperation(value = "获取品牌名字", notes = "获取品牌名字")
    public BaseResult getBrandNames(@RequestParam String materialName,@RequestParam String packageName) {
        try {
            Set<String> brandNames = fcProductService.getBrandNames(materialName, packageName);
            return BaseResult.successWithData(brandNames);
        } catch (Exception ex) {
            log.error("删除产品: error = {}", ex.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), HttpCode.INTERNAL_SERVER_ERROR.getMsg());
        }
    }

    @GetMapping("/getDefaultBom")
    @ApiOperation(value = "获取默认bom", notes = "获取默认bom")
    public BaseResult getDefaultBom(@RequestParam String packageName) {
        try {
            List<FcProduct> defaultBom = fcProductService.getDefaultBom(packageName);
            return BaseResult.successWithData(defaultBom);
        } catch (Exception ex) {
            log.error("获取默认bom: error = {}", ex.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), HttpCode.INTERNAL_SERVER_ERROR.getMsg());
        }
    }
}
