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


import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gci.boot.annotation.Query.handle.QueryGenerator;
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.common.vo.Result;
import com.gci.boot.modules.basedata.entity.TbRepairShopArchives;
import com.gci.boot.modules.basedata.service.ITbRepairShopArchivesService;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

/**
 * @author 24642
 */
@Api(tags="维修厂档案表对象")
@RestController
@RequestMapping("/base/repairShop")
@Slf4j
public class TbRepairShopArchivesController {
    @Autowired
    private ITbRepairShopArchivesService tbRepairShopArchivesService;

    /**
     * 分页列表查询
     *
     * @param archives
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AddSysLog(value = "维修厂档案表对象-分页列表查询")
    @ApiOperation(value="维修厂档案表对象-分页列表查询", notes="维修厂档案表对象-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(TbRepairShopArchives archives,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req ) {
        Page<TbRepairShopArchives> page = new Page<TbRepairShopArchives>(pageNo, pageSize);
        LambdaQueryWrapper<TbRepairShopArchives> eq = QueryGenerator.initQueryWrapper(TbRepairShopArchives.class, archives);
        IPage<TbRepairShopArchives> pageList = tbRepairShopArchivesService.page(page, eq);
        return Result.ok(pageList);
    }

    /**
     *   添加
     */
    @AddSysLog(value = "维修厂档案表对象-添加")
    @ApiOperation(value="维修厂档案表对象-添加", notes="维修厂档案表对象-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody @Validated(SaveValidated.class) TbRepairShopArchives archives) {
        archives.setType("1");
        tbRepairShopArchivesService.save(archives);
        return Result.ok("添加成功！");
    }

    /**
     *   编辑
     */
    @AddSysLog(value = "维修厂档案表对象-编辑")
    @ApiOperation(value="维修厂档案表对象-编辑", notes="维修厂档案表对象-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody @Validated(UpdateValidated.class) TbRepairShopArchives archives) {
        archives.setType("1");
        tbRepairShopArchivesService.updateById(archives);
        return Result.ok("编辑成功！");
    }

    /**
     *   删除
     */
    @AddSysLog(value = "维修厂档案表对象-删除")
    @ApiOperation(value="维修厂档案表对象-删除", notes="维修厂档案表对象-删除")
//    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name="id",required=true) String id) {
        tbRepairShopArchivesService.removeById(id);
        return Result.ok("删除成功！");
    }

    /**
     *   作废
     */
    @AddSysLog(value = "维修厂档案表对象-作废")
    @ApiOperation(value="维修厂档案表对象-作废", notes="维修厂档案表对象-作废")
    @DeleteMapping(value = "/cancel")
    public Result<?> cancel(@RequestParam(name="id",required=true) String id) {
        tbRepairShopArchivesService.update(new LambdaUpdateWrapper<TbRepairShopArchives>().eq(TbRepairShopArchives::getId,id).set(TbRepairShopArchives::getCancelFlag,1));
        return Result.ok("作废成功！");
    }

    /**
     *   恢复
     */
    @AddSysLog(value = "维修厂档案表对象-恢复")
    @ApiOperation(value="维修厂档案表对象-恢复", notes="维修厂档案表对象-恢复")
    @GetMapping(value = "/unCancel")
    public Result<?> unCancel(@RequestParam(name="id",required=true) String id) {
        tbRepairShopArchivesService.update(new LambdaUpdateWrapper<TbRepairShopArchives>().eq(TbRepairShopArchives::getId,id).set(TbRepairShopArchives::getCancelFlag,0));
        return Result.ok("恢复成功！");
    }

    /**
     *   批量删除
     */
    @AddSysLog(value = "维修厂档案表对象-批量删除")
    @ApiOperation(value="维修厂档案表对象-删除", notes="损失信息基础表-删除")
//    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        tbRepairShopArchivesService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * 导出
     * @return
     */
    @AddSysLog(value = "维修厂档案表对象-导出")
    @GetMapping("/export")
    public Result<?> export(TbRepairShopArchives tbRepairShopArchives, HttpServletResponse response){
        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);

            LambdaQueryWrapper<TbRepairShopArchives> lambdaQueryWrapper = QueryGenerator.initQueryWrapper(TbRepairShopArchives.class, tbRepairShopArchives);
            List<TbRepairShopArchives> list = tbRepairShopArchivesService.list(lambdaQueryWrapper);
            EasyExcel.write(output, TbRepairShopArchives.class).sheet("sheet1").doWrite(list);

        } catch (IOException e) {
            return Result.error("导出错误！");
        }
        return Result.ok();
    }
}
