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

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.handlinggroupuseramount.dto.StaffWagesPageDTO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.handlinggroupuseramount.vo.*;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.handlinggroupuseramount.HandlingGroupUserAmountDO;
import cn.iocoder.yudao.module.smartfactory.service.handlinggroupuseramount.HandlingGroupUserAmountService;
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.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/handling-group-user-amount")
@Validated
public class HandlingGroupUserAmountController {

    @Resource
    private HandlingGroupUserAmountService handlingGroupUserAmountService;

    @PostMapping("/createOrEdit")
    @Operation(summary = "创建搬运组每日个人工资记录")
    public CommonResult<Boolean> createOrEdit(@Valid @RequestBody HandlingGroupUserAmountSaveReqVO createReqVO) {
        handlingGroupUserAmountService.createOrEdit(createReqVO);
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新搬运组每日个人工资记录")
    public CommonResult<Boolean> updateHandlingGroupUserAmount(@Valid @RequestBody HandlingGroupUserAmountSaveReqVO updateReqVO) {
        handlingGroupUserAmountService.updateHandlingGroupUserAmount(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除搬运组每日个人工资记录")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteHandlingGroupUserAmount(@RequestParam("id") Long id) {
        handlingGroupUserAmountService.deleteHandlingGroupUserAmount(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得搬运组每日个人工资记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<HandlingGroupUserAmountRespVO> getHandlingGroupUserAmount(@RequestParam("id") Long id) {
        HandlingGroupUserAmountDO handlingGroupUserAmount = handlingGroupUserAmountService.getHandlingGroupUserAmount(id);
        return success(BeanUtils.toBean(handlingGroupUserAmount, HandlingGroupUserAmountRespVO.class));
    }


    @GetMapping("/getListByMonth")
    @Operation(summary = "获得搬运组工资记录 按月搜索")
    @Parameter(name = "time", description = "日期 格式(yyyy-MM) 默认当前月", required = true, example = "1024")
    @Parameter(name = "type", description = "类型 1按本月初到本月底 2按上月26号到本月25号 默认1", required = true, example = "1024")
    public CommonResult<HandlingGroupUserAmountMonthVO> getListByMonth(@RequestParam(value = "id", required = false) Long id,
                                                                       @RequestParam(value = "type", defaultValue = "1") Integer type,
                                                                       @RequestParam(value = "name", required = false) String name,
                                                                       @RequestParam(value = "time") String time,
                                                                       @RequestParam(value = "factoryId", required = false) Long factoryId) {
        HandlingGroupUserAmountMonthVO vo = handlingGroupUserAmountService.getListByMonth(id, factoryId, type, time, name);
        return success(vo);
    }

    @GetMapping("/page")
    @Operation(summary = "获得搬运组每日个人工资记录分页")
    public CommonResult<PageResult<HandlingGroupUserAmountRespVO>> getHandlingGroupUserAmountPage(@Valid HandlingGroupUserAmountPageReqVO pageReqVO) {
        PageResult<HandlingGroupUserAmountDO> pageResult = handlingGroupUserAmountService.getHandlingGroupUserAmountPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, HandlingGroupUserAmountRespVO.class));
    }

    @GetMapping("/listAll")
    @Operation(summary = "获得搬运组每日个人工资记录列表")
    public CommonResult<List<HandlingGroupUserAmountRespVO>> getHandlingGroupUserAmountList(@Valid HandlingGroupUserAmountPageReqVO pageReqVO) {
        List<HandlingGroupUserAmountDO> list = handlingGroupUserAmountService.getHandlingGroupUserAmountList(pageReqVO);
        return success(BeanUtils.toBean(list, HandlingGroupUserAmountRespVO.class));
    }

    @GetMapping("/staffWagesPage")
    @Operation(summary = "以用户分组工资分页列表")
    public CommonResult<PageResult<StaffWagesPageVO>> staffWagesPage(@Valid StaffWagesPageDTO dto) {
        PageResult<StaffWagesPageVO> pageResult = handlingGroupUserAmountService.staffWagesPage(dto);
        return success(pageResult);
    }

    @GetMapping("/confirm")
    @Operation(summary = "确认搬运组薪资")
    public CommonResult<Boolean> confirmHandlingGroupUserAmount(@RequestParam("factoryId") Long factoryId,
                                                                @RequestParam("month") String month) {

        handlingGroupUserAmountService.confirmHandlingGroupUserAmount(factoryId, month);
        return success(true);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出搬运组每日个人工资记录 Excel")
    @OperateLog(type = EXPORT)
    public void exportHandlingGroupUserAmountExcel(@Valid HandlingGroupUserAmountPageReqVO pageReqVO,
                                                   HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<HandlingGroupUserAmountDO> list = handlingGroupUserAmountService.getHandlingGroupUserAmountPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "搬运组每日个人工资记录.xls", "数据", HandlingGroupUserAmountRespVO.class,
                BeanUtils.toBean(list, HandlingGroupUserAmountRespVO.class));
    }

}
