package com.gci.boot.modules.finance.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.finance.entity.VehicleCost;
import com.gci.boot.modules.finance.service.IVehicleCostService;
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("/finance/vehicleCost")
@Slf4j
public class VehicleCostController {
    @Autowired
    private IVehicleCostService vehicleCostService;

    /**
     * 分页列表查询
     *
     */
    @AddSysLog("成本录入-分页列表查询")
    @ApiOperation(value="成本录入-分页列表查询", notes="成本录入-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(VehicleCost vehicleCost,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req ) {
        Page<VehicleCost> page = new Page<VehicleCost>(pageNo, pageSize);
        LambdaQueryWrapper<VehicleCost> eq = QueryGenerator.initQueryWrapper(VehicleCost.class, vehicleCost);
        IPage<VehicleCost> pageList = vehicleCostService.page(page, eq);
        return Result.ok(pageList);
    }

    /**
     *   添加
     */
    @AddSysLog("成本录入-新增")
    @ApiOperation(value="成本录入-添加", notes="成本录入-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody @Validated(SaveValidated.class) VehicleCost vehicleCost) {
        LambdaQueryWrapper<VehicleCost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VehicleCost::getVehicleType,vehicleCost.getVehicleType());
        if(vehicleCostService.count(queryWrapper) > 0){
            return Result.error(101,"已存在相同车型数据，请修改！");
        }
        vehicleCostService.save(vehicleCost);
        return Result.ok("添加成功！");
    }

    /**
     *   编辑
     */
    @AddSysLog("成本录入-编辑")
    @ApiOperation(value="成本录入-编辑", notes="成本录入-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody @Validated(UpdateValidated.class) VehicleCost vehicleCost) {
        LambdaQueryWrapper<VehicleCost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VehicleCost::getVehicleType,vehicleCost.getVehicleType())
                .ne(VehicleCost::getId,vehicleCost.getId());
        if(vehicleCostService.count(queryWrapper) > 0){
            return Result.error(101,"已存在相同车型数据，请修改！");
        }
        vehicleCostService.updateById(vehicleCost);
        return Result.ok("编辑成功！");
    }

    /**
     *   删除
     */
    @AddSysLog("成本录入-删除")
    @ApiOperation(value="成本录入-删除", notes="成本录入-删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name="id",required=true) String id) {
        vehicleCostService.removeById(id);
        return Result.ok("删除成功！");
    }

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

    /**
     * 导出
     * @return
     */
    @AddSysLog("成本录入-导出")
    @GetMapping("/export")
    public Result<?> export(VehicleCost 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<VehicleCost> lambdaQueryWrapper = QueryGenerator.initQueryWrapper(VehicleCost.class, tbRepairShopArchives);
            List<VehicleCost> list = vehicleCostService.list(lambdaQueryWrapper);
            EasyExcel.write(output, VehicleCost.class).sheet("成本录入").doWrite(list);

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