package net.lab1024.sa.admin.module.business.wms.base.customer.controller;

import net.lab1024.sa.admin.module.business.wms.base.customer.domain.entity.CustomerEntity;
import net.lab1024.sa.admin.module.business.wms.base.customer.domain.form.CustomerAddForm;
import net.lab1024.sa.admin.module.business.wms.base.customer.domain.form.CustomerQueryForm;
import net.lab1024.sa.admin.module.business.wms.base.customer.domain.form.CustomerSelect;
import net.lab1024.sa.admin.module.business.wms.base.customer.domain.form.CustomerUpdateForm;
import net.lab1024.sa.admin.module.business.wms.base.customer.domain.vo.CustomerVO;
import net.lab1024.sa.admin.module.business.wms.base.customer.service.CustomerQueryService;
import net.lab1024.sa.admin.module.business.wms.base.customer.service.CustomerService;
import net.lab1024.sa.admin.module.business.wms.base.customer.domain.entity.CustomerEntity;
import net.lab1024.sa.admin.module.business.wms.base.customer.domain.form.CustomerSelect;
import net.lab1024.sa.admin.module.business.wms.base.customer.service.CustomerService;
import net.lab1024.sa.base.common.domain.RequestUser;
import net.lab1024.sa.base.common.domain.ValidateList;
import net.lab1024.sa.base.common.util.SmartRequestUtil;
import net.lab1024.sa.base.module.support.operatelog.annotation.OperateLog;
import org.springframework.web.bind.annotation.*;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;

import java.util.List;

/**
 * 客户管理 Controller
 *
 * @author 霍锦
 * @since  2025-03-25 10:42:33
 * copyright 友仓
 */

@RestController
@Tag(name = "客户管理")
public class CustomerController {

    @Resource
    private CustomerService customerService;

    @Resource
    private CustomerQueryService customerQueryService;

    @Operation(summary = "分页查询 @author 霍锦")
    @PostMapping("/customer/queryPage")
    @SaCheckPermission("customer:query")
    public ResponseDTO<PageResult<CustomerVO>> queryPage(@RequestBody @Valid CustomerQueryForm queryForm) {
        return ResponseDTO.ok(customerQueryService.queryPage(queryForm));
    }

    @Operation(summary = "添加 @author 霍锦")
    @PostMapping("/customer/add")
    @SaCheckPermission("customer:add")
    @OperateLog
    public ResponseDTO<String> add(@RequestBody @Valid CustomerAddForm addForm) {
        RequestUser requestUser = SmartRequestUtil.getRequestUser();
        addForm.setCreateUserId(requestUser.getUserId());
        addForm.setCreateUserName(requestUser.getUserName());
        return customerService.add(addForm);
    }

    @Operation(summary = "更新 @author 霍锦")
    @PostMapping("/customer/update")
    @SaCheckPermission("customer:update")
    @OperateLog
    public ResponseDTO<String> update(@RequestBody @Valid CustomerUpdateForm updateForm) {
        return customerService.update(updateForm);
    }

    @Operation(summary = "批量删除 @author 霍锦")
    @PostMapping("/customer/batchDelete")
    @SaCheckPermission("customer:batchDelete")
    @OperateLog
    public ResponseDTO<String> batchDelete(@RequestBody ValidateList<Long> idList) {
        return customerService.batchDelete(idList);
    }

    @Operation(summary = "单个删除 @author 霍锦")
    @GetMapping("/customer/delete")
    @SaCheckPermission("customer:delete")
    @OperateLog
    public ResponseDTO<String> delete(@RequestParam Long customerId) {
        return customerService.delete(customerId);
    }

    @Operation(summary = "客户下拉查询")
    @PostMapping("/customer/queryCustomer")
    public ResponseDTO<List<CustomerEntity>> queryCustomer(@RequestBody CustomerSelect customerSelect) {
        return ResponseDTO.ok(customerQueryService.queryCustomer(customerSelect));
    }
}
