package com.zksy.park.controller;


import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksy.common.annotation.Log;
import com.zksy.common.core.domain.Result;
import com.zksy.common.enums.BusinessType;
import com.zksy.common.utils.SearchUtil;
import com.zksy.park.domain.SmartEmployment;
import com.zksy.park.service.SmartEmploymentService;
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.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 智慧用工
 */
@Slf4j
@RestController
@RequestMapping("/smartEmployment")
@Api(tags = "智慧用工信息", description = "智慧用工信息")
public class SmartEmploymentController {

    @Autowired
    private SmartEmploymentService service;

    @GetMapping("/getById/{id}")
    @ApiOperation(value = "智慧用工信息搜索getById")
    public Result getById(@PathVariable String id) {
        return Result.ok(service.getById(id));
    }

    @GetMapping("/findByPage")
    @ApiOperation(value = "智慧用工信息分页")
    public Result findByPage(long pageNum, long pageSize, String conditionJson) throws Exception {
        return Result.ok(service.page(new Page<>(pageNum, pageSize), SearchUtil.parseWhereSql(conditionJson)));
    }

    @GetMapping("/getList")
    @ApiOperation(value = "查询所有智慧用工信息")
    public Result getList(String conditionJson) throws Exception {
        return Result.ok(service.list(SearchUtil.parseWhereSql(conditionJson)));
    }

    @PostMapping("/save")
    @ApiOperation(value = "新增智慧用工信息")
    @Log(title = "新增智慧用工信息", businessType = BusinessType.INSERT)
    public Result<Object> save(@RequestBody SmartEmployment entity) {
        entity.setCreateTime(new DateTime().toLocalDateTime());
        entity.setUpdateTime(new DateTime().toLocalDateTime());
        return Result.ok(service.save(entity),"新增成功");
    }

    @PostMapping("/saveBatch")
    @ApiOperation(value = "智慧用工信息批量新增")
    @Log(title = "批量新增智慧用工信息", businessType = BusinessType.INSERT)
    public Result<Object> saveBatch(@RequestBody List<SmartEmployment> entityList) {
        entityList.forEach(q->{
            q.setUpdateTime(new DateTime().toLocalDateTime());
            q.setCreateTime(new DateTime().toLocalDateTime());
        });
        return Result.ok(service.saveBatch(entityList),"新增成功");
    }

    @PutMapping("/updateById")
    @ApiOperation(value = "智慧用工信息修改")
    @Log(title = "修改智慧用工信息", businessType = BusinessType.UPDATE)
    public Result<Object> updateById(@RequestBody SmartEmployment entity) {
        entity.setUpdateTime(new DateTime().toLocalDateTime());
        return Result.ok(service.updateById(entity),"修改成功");
    }

    @DeleteMapping("/deleteById")
    @ApiOperation(value = "智慧用工信息删除")
    @Log(title = "删除智慧用工信息", businessType = BusinessType.DELETE)
    public Result<Object> deleteById(String id) {
        return Result.ok(service.removeById(id),"删除成功");
    }

    @DeleteMapping("/deleteBatchById")
    @ApiOperation(value = "智慧用工信息批量删除")
    @Log(title = "智慧用工信息批量删除", businessType = BusinessType.DELETE)
    public Result<Object> deleteBatchById(@RequestParam List<String> ids) {
        return Result.ok(service.removeByIds(ids),"删除成功");
    }

    @GetMapping("/exportExcel")
    @ApiOperation(value = "导出智慧用工信息Excel")
    @Log(title = "导出智慧用工信息Excel", businessType = BusinessType.EXPORT)
    public void exportExcel(HttpServletResponse response) throws IOException {
        service.exportExcel(response);
    }

    @PostMapping("/importExcel")
    @ApiOperation(value = "导入智慧用工信息Excel")
    @Log(title = "导入智慧用工信息Excel", businessType = BusinessType.IMPORT)
    public Result<Object> importExcel(@RequestParam("file") MultipartFile file) throws IOException {
        return service.importExcel(file);
    }


}
