package org.dromara.project.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.project.domain.exportvo.BusConstructionUserExportVo;
import org.dromara.project.domain.dto.constructionuser.*;
import org.dromara.project.domain.vo.constructionuser.BusConstructionUserAttendanceMonthVo;
import org.dromara.project.domain.vo.constructionuser.BusConstructionUserAttendanceTotalVo;
import org.dromara.project.domain.vo.constructionuser.BusConstructionUserGisVo;
import org.dromara.project.domain.vo.constructionuser.BusConstructionUserVo;
import org.dromara.project.service.IBusConstructionUserService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 施工人员
 *
 * @author lcj
 * @date 2025-03-07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/project/constructionUser")
public class BusConstructionUserController extends BaseController {

    private final IBusConstructionUserService busConstructionUserService;

    /**
     * 查询施工人员列表
     */
    @SaCheckPermission("project:constructionUser:list")
    @GetMapping("/list")
    public TableDataInfo<BusConstructionUserVo> list(BusConstructionUserQueryReq req, PageQuery pageQuery) {
        return busConstructionUserService.queryPageList(req, pageQuery);
    }

    /**
     * 查询每个施工人员总的考勤列表
     */
    @SaCheckPermission("project:attendance:list")
    @GetMapping("/list/attendance/total")
    public TableDataInfo<BusConstructionUserAttendanceTotalVo> listAttendanceTotal(BusConstructionUserAttendanceQueryReq req,
                                                                                   PageQuery pageQuery) {
        return busConstructionUserService.queryPageAttendanceList(req, pageQuery);
    }

    /**
     * 查询施工人员月份考勤列表
     */
    @SaCheckPermission("project:constructionUser:list")
    @GetMapping("/list/attendance/month")
    public R<List<BusConstructionUserAttendanceMonthVo>> listAttendanceMonth(BusConstructionUserAttendanceMonthReq req) {
        return R.ok(busConstructionUserService.queryAttendanceMonthList(req));
    }

    /**
     * 导出施工人员列表
     */
    @SaCheckPermission("project:constructionUser:export")
    @Log(title = "施工人员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BusConstructionUserQueryReq req, HttpServletResponse response) {
        List<BusConstructionUserExportVo> list = busConstructionUserService.queryList(req);
        ExcelUtil.exportExcel(list, "施工人员", BusConstructionUserExportVo.class, response);
    }

    /**
     * 获取施工人员详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("project:constructionUser:query")
    @GetMapping("/{id}")
    public R<BusConstructionUserVo> getInfo(@NotNull(message = "主键不能为空")
                                            @PathVariable Long id) {
        return R.ok(busConstructionUserService.queryById(id));
    }

    /**
     * 查询大屏施工人员信息
     */
    @SaCheckPermission("project:constructionUser:query")
    @GetMapping("/gis")
    public R<BusConstructionUserGisVo> getGisData(BusConstructionUserGisReq req) {
        return R.ok(busConstructionUserService.getGisData(req));
    }

    /**
     * 新增施工人员
     */
    @SaCheckPermission("project:constructionUser:add")
    @Log(title = "施工人员", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Long> add(@Validated(AddGroup.class) @RequestBody BusConstructionUserCreateReq req) {
        return R.ok(busConstructionUserService.insertByBo(req));
    }

    /**
     * 修改施工人员
     */
    @SaCheckPermission("project:constructionUser:edit")
    @Log(title = "施工人员", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BusConstructionUserUpdateReq req) {
        return toAjax(busConstructionUserService.updateByBo(req));
    }

    /**
     * 修改施工人员工资
     */
    @SaCheckPermission("project:constructionUser:edit")
    @Log(title = "施工人员", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/salary")
    public R<Void> editSalary(@Validated(EditGroup.class) @RequestBody BusConstructionUserUpdateSalaryReq req) {
        return toAjax(busConstructionUserService.updateSalary(req));
    }

    /**
     * 修改施工人员打卡状态
     */
    @SaCheckPermission("project:constructionUser:edit")
    @Log(title = "施工人员", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/clock")
    public R<Void> editClock(@Validated(EditGroup.class) @RequestBody BusConstructionUserUpdateClockReq req) {
        return toAjax(busConstructionUserService.updateClock(req));
    }

    /**
     * 批量修改施工人员状态
     */
    @SaCheckPermission("project:constructionUser:edit")
    @Log(title = "施工人员", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/batch/status")
    public R<Void> batchUpdateStatus(@Validated(EditGroup.class) @RequestBody BusConstructionUserBatchUpdateStatusReq req) {
        return toAjax(busConstructionUserService.batchUpdateStatus(req));
    }

    /**
     * 根据项目id批量修改施工人员打卡状态
     */
    @SaCheckPermission("project:constructionUser:edit")
    @Log(title = "施工人员", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/batch/clock")
    public R<Void> batchUpdateClock(@Validated(EditGroup.class) @RequestBody BusConstructionUserBatchUpdateClockReq req) {
        return toAjax(busConstructionUserService.batchUpdateClockByProjectId(req));
    }

    /**
     * 施工人员迁移
     */
    @SaCheckPermission("project:constructionUser:edit")
    @Log(title = "施工人员", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/change/project")
    public R<Void> changeUserProject(@Validated(EditGroup.class) @RequestBody BusConstructionUserChangeProjectReq req) {
        return toAjax(busConstructionUserService.changeUserProject(req));
    }

    /**
     * 删除施工人员
     *
     * @param ids 主键串
     */
    @SaCheckPermission("project:constructionUser:remove")
    @Log(title = "施工人员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(busConstructionUserService.deleteWithValidByIds(List.of(ids), true));
    }
}
