package cn.iocoder.yudao.module.smartfactory.controller.admin.staffsalary;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.smartfactory.controller.admin.staffsalary.vo.SalaryTotalVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.staffsalary.vo.StaffSalaryPageReqVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.staffsalary.vo.StaffSalaryRespVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.staffsalary.vo.StaffSalarySaveReqVO;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.staffsalary.StaffSalaryDO;
import cn.iocoder.yudao.module.smartfactory.service.staffsalary.StaffSalaryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 厂区员工工资")
@RestController
@RequestMapping("/smartfactory/staff-salary")
@Validated
public class StaffSalaryController {

    @Resource
    private StaffSalaryService staffSalaryService;

    @PostMapping("/create")
    @Operation(summary = "创建员工工资")
    public CommonResult<Long> createStaff(@Valid @RequestBody StaffSalarySaveReqVO createReqVO) {
        return success(staffSalaryService.createStaffSalary(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新厂区员工工资")
    public CommonResult<Boolean> updateStaffSalary(@Valid @RequestBody StaffSalarySaveReqVO updateReqVO) {
        staffSalaryService.updateStaffSalary(updateReqVO);
        return success(true);
    }

    @PutMapping("/update-status")
    @Operation(summary = "更新厂区员工工资状态")
    public CommonResult<Boolean> updateStatus(@RequestBody StaffSalarySaveReqVO updateReqVO) {
        staffSalaryService.updateStatus(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除厂区员工工资")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteStaffSalary(@RequestParam("id") Long id) {
        staffSalaryService.deleteStaffSalary(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得厂区员工工资")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<StaffSalaryRespVO> getStaffSalary(@RequestParam("id") Long id) {
        StaffSalaryDO staffSalary = staffSalaryService.getStaffSalary(id);

        return success(BeanUtils.toBean(staffSalary, StaffSalaryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得厂区员工工资分页")
    public CommonResult<PageResult<StaffSalaryRespVO>> getStaffSalaryPage(@Valid StaffSalaryPageReqVO pageReqVO) {
        PageResult<StaffSalaryRespVO> pageResult = staffSalaryService.getStaffSalaryPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/total")
    @Operation(summary = "获取工资统计")
    public CommonResult<SalaryTotalVO> getStaffSalaryTotal(@Valid StaffSalaryPageReqVO pageReqVO) {

        return success(staffSalaryService.getStaffSalaryTotal(pageReqVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出厂区员工工资 Excel")
    @OperateLog(type = EXPORT)
    public void exportStaffSalaryExcel(@Valid StaffSalaryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StaffSalaryRespVO> list = staffSalaryService.getStaffSalaryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "厂区员工工资.xls", "数据", StaffSalaryRespVO.class,
                list);
    }
}
