package com.fuyuanshen.customer.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuyuanshen.common.core.domain.R;
import com.fuyuanshen.common.core.domain.ResponseVO;
import com.fuyuanshen.common.core.utils.StringUtils;
import com.fuyuanshen.common.mybatis.core.page.TableDataInfo;
import com.fuyuanshen.customer.domain.Customer;
import com.fuyuanshen.customer.domain.query.UserQueryCriteria;
import com.fuyuanshen.customer.domain.vo.ConsumerVo;
import com.fuyuanshen.customer.service.CustomerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.undertow.util.BadRequestException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @author: 默苍璃
 * @date: 2025-07-0114:20
 */
@Slf4j
@Tag(name = "WEB：客户管理", description = "WEB：客户管理")
@RestController
@RequestMapping("/api/customers")
@RequiredArgsConstructor
public class CustomerController {

    private final CustomerService customerService;


    /**
     * 分页查询客户(限制子客户)
     *
     * @param criteria
     * @return
     */
    @Operation(summary = "分页查询客户")
    @GetMapping(value = "/customer")
    public TableDataInfo<ConsumerVo> queryCustomer(UserQueryCriteria criteria) {
        Page<Customer> page = new Page<>(criteria.getPageNum(), criteria.getPageSize());
        return customerService.queryCustomers(criteria, page);
    }


    @GetMapping(value = "/allCustomer")
    @Operation(summary = "查询所有客户")
    public R<List<Customer>>  queryAllCustomer(UserQueryCriteria criteria) {
        List<Customer> customers = customerService.queryAllCustomers(criteria);
        return R.ok(customers);
    }


    // @Log("新增客户")
    @Operation(summary = "新增客户")
    @PostMapping(value = "/addCustomer")
    public R<Void>  addCustomer(@Validated @RequestBody Customer customer) throws BadRequestException {
        if (StringUtils.isBlank(customer.getPassword())) {
            throw new BadRequestException("账号密码不能为空");
        }
        customer.setPassword(BCrypt.hashpw(customer.getPassword()));
        customerService.addCustomer(customer);
        return R.ok();
    }


    // @Log("修改客户")
    @Operation(summary = "修改客户")
    @PutMapping(value = "updateCustomer")
    public R<Void>  updateCustomer(@RequestBody Customer resources) throws Exception {
        customerService.updateCustomer(resources);
        return R.ok();
    }


    // @Log("删除客户")
    @Operation(summary = "删除客户")
    @DeleteMapping(value = "/deleteCustomer")
    public R<Void> deleteCustomer(@RequestBody Set<Long> ids) throws BadRequestException {
        if (CollectionUtil.isEmpty(ids)) {
            throw new BadRequestException("请选择要删除的客户");
        }
        customerService.delete(ids);
        return R.ok();
    }


}
