package org.dromara.work.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.work.domain.bo.TpClientBo;
import org.dromara.work.domain.vo.ClientSumVo;
import org.dromara.work.domain.vo.TpClientVo;
import org.dromara.work.service.ITpClientService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 客户
 *
 * @author Maosw
 * @date 2024-08-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/work/client")
public class TpClientController extends BaseController {

    private final ITpClientService tpClientService;

    /**
     * 查询客户列表
     */
    @SaCheckPermission("work:client:list")
    @GetMapping("/list")
    public TableDataInfo<TpClientVo> list(TpClientBo bo, PageQuery pageQuery) {
        return tpClientService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询客户下单金额统计列表
     */
    @SaCheckPermission("work:client:listChart")
    @GetMapping("/listChart")
    public TableDataInfo<ClientSumVo> listChart(TpClientBo bo, PageQuery pageQuery) {
        return tpClientService.queryPageListChart(bo, pageQuery);
    }

    /**
     * 导出客户列表
     */
    @SaCheckPermission("work:client:export")
    @Log(title = "导出客户列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TpClientBo bo, HttpServletResponse response) {
        List<TpClientVo> list = tpClientService.queryList(bo);
        ExcelUtil.exportExcel(list, "导出客户列表", TpClientVo.class, response);
    }

    /**
     * 获取客户详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("work:client:query")
    @GetMapping("/{id}")
    public R<TpClientVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tpClientService.queryById(id));
    }

    /**
     * 新增客户
     */
    @SaCheckPermission("work:client:add")
    @Log(title = "客户", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TpClientBo bo) {
        return toAjax(tpClientService.insertByBo(bo));
    }

    /**
     * 修改客户
     */
    @SaCheckPermission("work:client:edit")
    @Log(title = "客户", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TpClientBo bo) {
        return toAjax(tpClientService.updateByBo(bo));
    }

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