package com.willalp.web.controller.clockingin;

import com.willalp.common.annotation.Log;
import com.willalp.common.core.controller.BaseController;
import com.willalp.common.core.domain.AjaxResult;
import com.willalp.common.core.page.TableDataInfo;
import com.willalp.common.enums.BusinessType;
import com.willalp.common.utils.DateUtils;
import com.willalp.common.utils.SecurityUtils;
import com.willalp.common.utils.poi.ExcelUtil;
import com.willalp.salary.domain.HsSalarySlip;
import com.willalp.salary.service.IHsSalarySlipService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 工资条基础Controller
 *
 * @author willalp
 * @date 2022-07-26
 */
@RestController
@RequestMapping("/salary/salaryslip")
public class HsSalarySlipController extends BaseController {
    @Resource
    private IHsSalarySlipService hsSalarySlipService;


    /**
     * 查询工资条基础列表
     */
    @GetMapping("/excelList")
    public TableDataInfo excelList(HsSalarySlip hsSalarySlip) {
        startPage();
        List<HsSalarySlip> list = hsSalarySlipService.excelList(hsSalarySlip);
        return getDataTable(list);
    }

    /**
     * 查询工资条基础列表
     */
    @PreAuthorize("@ss.hasPermi('salary:salaryslip:list')")
    @GetMapping("/list")
    public TableDataInfo list(HsSalarySlip hsSalarySlip) {
        startPage();
        List<HsSalarySlip> list = hsSalarySlipService.selectHsSalarySlipList(hsSalarySlip);
        return getDataTable(list);
    }

    /**
     * 导出工资条基础列表
     */
    @PreAuthorize("@ss.hasPermi('salary:salaryslip:export')")
    @Log(title = "工资条基础", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HsSalarySlip hsSalarySlip) {
        List<HsSalarySlip> list = hsSalarySlipService.selectHsSalarySlipList(hsSalarySlip);
        ExcelUtil<HsSalarySlip> util = new ExcelUtil<HsSalarySlip>(HsSalarySlip.class);
        return util.exportExcel(list, "工资条基础数据");
    }

    @Log(title = "工资条基础", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<HsSalarySlip> util = new ExcelUtil<HsSalarySlip>(HsSalarySlip.class);
        //获取数据库对应数据
        String filename = file.getOriginalFilename();
        List<HsSalarySlip> list = util.importExcel(file.getInputStream());
        return AjaxResult.success(hsSalarySlipService.importData(list, util, file.getInputStream(), filename));
    }

    @GetMapping("/importTemp")
    public AjaxResult importTemp() {
        ExcelUtil<HsSalarySlip> util = new ExcelUtil<>(HsSalarySlip.class);
        return util.importTemplateExcel("工资条数据导入模板");
    }


    /**
     * 获取工资条详细信息
     */
    @PreAuthorize("@ss.hasPermi('salary:salaryslip:query')")
    @GetMapping(value = "/s/{id}")
    public AjaxResult getInfos(@PathVariable("id") String id) {
        return AjaxResult.success(hsSalarySlipService.getById(id).getZend());
    }


    /**
     * 获取工资条基础信息
     */
    @PreAuthorize("@ss.hasPermi('salary:salaryslip:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(hsSalarySlipService.getById(id));
    }

    /**
     * 新增工资条基础
     */
    @PreAuthorize("@ss.hasPermi('salary:salaryslip:add')")
    @Log(title = "工资条基础", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HsSalarySlip hsSalarySlip) {
        hsSalarySlip.setCreateBy(SecurityUtils.getUsername());
        hsSalarySlip.setCreateTime(DateUtils.getNowDate());
        return toAjax(hsSalarySlipService.save(hsSalarySlip));
    }

    /**
     * 修改工资条基础
     */
    @PreAuthorize("@ss.hasPermi('salary:salaryslip:edit')")
    @Log(title = "工资条基础", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HsSalarySlip hsSalarySlip) {

        hsSalarySlip.setUpdateBy(SecurityUtils.getUsername());
        hsSalarySlip.setUpdateTime(DateUtils.getNowDate());
        return toAjax(hsSalarySlipService.updateById(hsSalarySlip));
    }

    /**
     * 删除工资条基础
     */
    @PreAuthorize("@ss.hasPermi('salary:salaryslip:remove')")
    @Log(title = "工资条基础", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(hsSalarySlipService.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 发布
     */
    @Log(title = "工资条基础", businessType = BusinessType.DELETE)
    @PostMapping("/push")
    public AjaxResult push(@RequestBody String[] ids) {
        hsSalarySlipService.updatePushStatus(ids);
        return AjaxResult.success();
    }
}
