package org.dromara.mp.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import org.dromara.common.log.enums.OperatorType;
import org.dromara.common.satoken.utils.MpLoginHelper;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
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.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.mp.domain.vo.UserWorkCheckinVo;
import org.dromara.mp.domain.bo.UserWorkCheckinBo;
import org.dromara.mp.service.IUserWorkCheckinService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 自雇者打卡记录
 * 前端访问路由地址为:/mp/workCheckin
 *
 * @author LionLi
 * @date 2024-04-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/workCheckin")
public class UserWorkCheckinController extends BaseController {

    private final IUserWorkCheckinService userWorkCheckinService;

    /**
     * 查询自雇者打卡记录列表
     */
    @GetMapping("/list")
    @Log(title = "查询自雇者打卡记录列表", businessType = BusinessType.SELECT,operatorType= OperatorType.MOBILE)
    public R<List<UserWorkCheckinVo>> list(UserWorkCheckinBo bo, PageQuery pageQuery) {
        bo.setUserWorkId(MpLoginHelper.getUserId());
        return R.ok(userWorkCheckinService.queryList(bo));
    }

    /**
     * 导出自雇者打卡记录列表
     */
    @Log(title = "自雇者打卡记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(UserWorkCheckinBo bo, HttpServletResponse response) {
        List<UserWorkCheckinVo> list = userWorkCheckinService.queryList(bo);
        ExcelUtil.exportExcel(list, "自雇者打卡记录", UserWorkCheckinVo.class, response);
    }

    /**
     * 获取自雇者打卡记录详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    @Log(title = "获取自雇者打卡记录详细信息", businessType = BusinessType.SELECT,operatorType= OperatorType.MOBILE)
    public R<UserWorkCheckinVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(userWorkCheckinService.queryById(id));
    }

    /**
     * 新增自雇者打卡记录
     */
    @Log(title = "自雇者打卡记录", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody UserWorkCheckinBo bo) {
        bo.setUserWorkId(MpLoginHelper.getUserId());
        return toAjax(userWorkCheckinService.insertByBo(bo));
    }

    /**
     * 修改自雇者打卡记录
     */
    @Log(title = "自雇者打卡记录", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody UserWorkCheckinBo bo) {
        return toAjax(userWorkCheckinService.updateByBo(bo));
    }

    /**
     * 删除自雇者打卡记录
     *
     * @param ids 主键串
     */
    @Log(title = "自雇者打卡记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(userWorkCheckinService.deleteWithValidByIds(List.of(ids), true));
    }
}
