package com.zzsmart.qomo.kn.cost.manage.controller;

import java.io.InputStream;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zzsmart.qomo.kn.cost.manage.entity.CostStandard;
import com.zzsmart.qomo.kn.cost.manage.service.ICostStandardService;
import com.zzsmart.qomo.kn.cost.manage.vo.CostStandardVO;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.system.entity.SysUser;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: 标准成本
 * @Author: wangqiong
 * @Date:   2024-06-13
 * @Version: V1.0
 */
@Api(tags="标准成本计算")
@RestController
@RequestMapping("//costStandard")
@Slf4j
public class CostStandardController extends JeecgController<CostStandard, ICostStandardService> {
	@Autowired
	private ICostStandardService costStandardService;
	
	/**
	 * 分页列表查询
	 *
	 * @param costStandard
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "cost_standard-分页列表查询")
	@ApiOperation(value="cost_standard-分页列表查询", notes="cost_standard-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<CostStandardVO>> queryPageList(CostStandardVO costStandard,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {

		Page<CostStandardVO> page = new Page<CostStandardVO>(pageNo, pageSize);
		IPage<CostStandardVO> pageList = costStandardService.pageList(page, costStandard);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param costStandard
	 * @return
	 */
	@AutoLog(value = "cost_standard-添加")
	@ApiOperation(value="cost_standard-添加", notes="cost_standard-添加")
//	@RequiresPermissions(":cost_standard:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody CostStandard costStandard) {
		costStandardService.save(costStandard);
		return Result.OK("添加成功！");
	}


	 /**
	 *  编辑
	 *
	 * @param costStandard
	 * @return
	 */
	@AutoLog(value = "cost_standard-编辑")
	@ApiOperation(value="cost_standard-编辑", notes="cost_standard-编辑")
//	@RequiresPermissions(":cost_standard:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody CostStandard costStandard) {
		costStandardService.updateById(costStandard);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "cost_standard-通过id删除")
	@ApiOperation(value="cost_standard-通过id删除", notes="cost_standard-通过id删除")
//	@RequiresPermissions(":cost_standard:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		costStandardService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "cost_standard-批量删除")
	@ApiOperation(value="cost_standard-批量删除", notes="cost_standard-批量删除")
//	@RequiresPermissions(":cost_standard:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.costStandardService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "cost_standard-通过id查询")
	@ApiOperation(value="cost_standard-通过id查询", notes="cost_standard-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<CostStandard> queryById(@RequestParam(name="id",required=true) String id) {
		CostStandard costStandard = costStandardService.getById(id);
		if(costStandard==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(costStandard);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param costStandard
    */
//    @RequiresPermissions(":cost_standard:exportXls")
    @RequestMapping(value = "/exportXls", method = RequestMethod.GET)
    public ModelAndView exportXls(HttpServletRequest request, CostStandard costStandard) {
        return super.exportXls(request, costStandard, CostStandard.class, "cost_standard");
    }

    /**
      * 通过excel导入数据并进行批量计算
    *
    * @param request
    * @param response
    * @return
    */
//    @RequiresPermissions(":cost_standard:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CostStandard.class);
    }

	 @GetMapping("/download/excel-template")
	 public void downloadExcel(HttpServletResponse response) throws IOException {
		 // 创建工作簿
		 Workbook workbook = new XSSFWorkbook();
		 Sheet sheet = workbook.createSheet("生变成版本号导入模板");

		 // 创建表头行
		 Row headerRow = sheet.createRow(0);
		 // 设置字体
		 Font headerFont = workbook.createFont();
		 headerFont.setBold(true); // 设置字体为加粗
		// headerFont.setFontHeightInPoints((short) 14); // 设置字体大小

		 CellStyle headerCellStyle = workbook.createCellStyle();
		 headerCellStyle.setFont(headerFont);
		 Cell firstCell = headerRow.createCell(0);
		 firstCell.setCellStyle(headerCellStyle);
		 firstCell.setCellValue("物料号");
		 Cell secondCell = headerRow.createCell(1);
		 secondCell.setCellStyle(headerCellStyle);
		 secondCell.setCellValue("图号");
		 Cell thirdCell = headerRow.createCell(2);
		 thirdCell.setCellStyle(headerCellStyle);
		 thirdCell.setCellValue("项目阶段");

		 // 设置下拉列表数据
		 DataValidationHelper validationHelper = sheet.getDataValidationHelper();
		 DataValidationConstraint constraint = validationHelper.createExplicitListConstraint(
				 new String[]{"初始","首批","量产"}); // 下拉列表的选项

		 // 应用下拉列表到第三列（索引为2）
		 CellRangeAddressList addressList = new CellRangeAddressList(1, 65535, 2, 2); // 从第二行开始到第101行，第三列
		 DataValidation validation = validationHelper.createValidation(constraint, addressList);
		 sheet.addValidationData(validation);



		 // 设置响应头
		 response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
		 response.setHeader("Content-Disposition", "attachment; filename=example-template.xlsx");

		 // 将工作簿写入输出流
		 workbook.write(response.getOutputStream());
		 workbook.close();

		 response.flushBuffer();
	 }

}
