package com.gci.boot.modules.basedata.controller;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.gci.boot.common.log.annotation.AddSysLog;
import com.gci.boot.common.validated.SaveValidated;
import com.gci.boot.common.validated.UpdateValidated;
import com.gci.boot.modules.basedata.entity.TbCustomerArchives;
import com.gci.boot.modules.basedata.service.ITbCustomerFollowTemplateService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import com.gci.boot.common.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import com.gci.boot.modules.basedata.entity.TbCustomerFollowTemplate;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 客户回访模板 前端控制器
 * </p>
 *
 * @author 2464
 * @since 2023-10-11
 */
@RestController
@RequestMapping("/basedata/customerFollowTemplate")
public class TbCustomerFollowTemplateController {
    @Autowired
    private ITbCustomerFollowTemplateService iTbCustomerFollowTemplateService;

    /**
     * 分页列表查询
     *
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AddSysLog(value = "客户回访模板-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(TbCustomerFollowTemplate customerFollowTemplate,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            HttpServletRequest req) {
        Page<TbCustomerFollowTemplate> page = new Page<TbCustomerFollowTemplate>(pageNo, pageSize);
        IPage<TbCustomerFollowTemplate> pageList = iTbCustomerFollowTemplateService.pageBuild(page, customerFollowTemplate);
        return Result.ok(pageList);
    }

    /**
     * 添加
     */
    @AddSysLog(value = "客户回访模板-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody @Validated(SaveValidated.class) TbCustomerFollowTemplate tbCustomerFollowTemplate) {
        iTbCustomerFollowTemplateService.saveItem(tbCustomerFollowTemplate);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     */
    @AddSysLog(value = "客户回访模板-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody @Validated(UpdateValidated.class) TbCustomerFollowTemplate tbCustomerFollowTemplate) {
        iTbCustomerFollowTemplateService.updateItem(tbCustomerFollowTemplate);
        return Result.ok("编辑成功！");
    }

    /**
     * 删除
     */
    @AddSysLog(value = "客户回访模板-删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        iTbCustomerFollowTemplateService.removeItem(id);
        return Result.ok("删除成功！");
    }

    /**
     * 批量删除
     */
    @AddSysLog(value = "客户回访模板-批量删除")
//    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        iTbCustomerFollowTemplateService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     *  导出
     */
    @AddSysLog(value = "客户回访模板-导出")
    @GetMapping("/export")
    public Result<?> export(HttpServletResponse response, TbCustomerFollowTemplate customerFollowTemplate){
        try (OutputStream output = response.getOutputStream()){
            String fileName = "客户回访模板.xlsx";
            try {
                fileName = URLEncoder.encode(fileName, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ";" + "filename*=utf-8''" + fileName);
            Page<TbCustomerFollowTemplate> page = new Page<TbCustomerFollowTemplate>(1, -1);
            IPage<TbCustomerFollowTemplate> pageList = iTbCustomerFollowTemplateService.pageBuild(page, customerFollowTemplate);
            Workbook sheets = ExcelExportUtil.exportExcel(new ExportParams(), TbCustomerFollowTemplate.class, pageList.getRecords());
            sheets.write(output);
        } catch (IOException e) {
            return Result.error("导出错误！");
        }
        return Result.ok();
    }
}
