package cn.iocoder.yudao.module.custom.controller.admin;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.custom.entity.CustomerMain;
import cn.iocoder.yudao.module.custom.service.CustomerMainService;
import cn.iocoder.yudao.module.custom.vo.CustomerMainPageReqVO;
import cn.iocoder.yudao.module.custom.vo.CustomerMainRespVO;
import cn.iocoder.yudao.module.custom.vo.CustomerMainSaveReqVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 客户管理（每个租户的下的客户信息）
 *
 * @author : http://www.chiner.pro
 * @date : 2024-7-31
 */
@RestController
@RequestMapping("/base/custom")
@Tag(name = "管理后台 - BASE 企业管理")
@Validated
public class CustomerMainController {
    @Resource
    private CustomerMainService customerMainService;

    /**
     * 客户管理分页列表查询
     *
     * @param pageReqVO 客户管理查询条件封装实体
     * @param pageNo    分页参数
     * @param pageSize  分页参数
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<com.baomidou.mybatisplus.core.metadata.IPage < ?>>
     * @author PQZ
     * @date 14:56 2024/8/1
     **/
    @GetMapping("/page")
    @Operation(summary = "获得客户管理分页")
    @PreAuthorize("@ss.hasPermission('base:customer-main:query')")
    public CommonResult<IPage<?>> getCustomerMainPage(CustomerMainPageReqVO pageReqVO,
                                                      @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<CustomerMainRespVO> page = new Page<>(pageNo, pageSize);
        return success(customerMainService.queryListPage(pageReqVO, page));
    }

    /**
     * 新增客户
     *
     * @param saveReqVO 保存客户信息扩展实体
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<java.lang.Boolean>
     * @author PQZ
     * @date 15:42 2024/8/1
     **/
    @PostMapping("/create")
    @Operation(summary = "创建客户管理")
    @PreAuthorize("@ss.hasPermission('base:customer-main:create')")
    public CommonResult<Boolean> createCustomerMain(@Valid @RequestBody CustomerMainSaveReqVO saveReqVO) {
        customerMainService.saveCustomer(saveReqVO);
        return success(true);
    }

    /**
     * 编辑客户
     *
     * @param saveReqVO 保存客户信息扩展实体
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<java.lang.Boolean>
     * @author PQZ
     * @date 15:44 2024/8/1
     **/
    @PutMapping("/update")
    @Operation(summary = "更新客户管理")
    @PreAuthorize("@ss.hasPermission('base:customer-main:update')")
    public CommonResult<Boolean> updateCustomerMain(@Valid @RequestBody CustomerMainSaveReqVO saveReqVO) {
        customerMainService.saveCustomer(saveReqVO);
        return success(true);
    }

    /**
     * 删除客户
     *
     * @param id 保存客户信息扩展实体
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<java.lang.Boolean>
     * @author PQZ
     * @date 15:44 2024/8/1
     **/
    @DeleteMapping("/delete")
    @Operation(summary = "删除客户管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('base:customer-main:delete')")
    public CommonResult<Boolean> deleteCustomerMain(@RequestParam("id") String id) {
        return success(true);
    }

    /**
     * 通过客户id查询客户信息
     *
     * @param id 客户id
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<java.lang.Boolean>
     * @author PQZ
     * @date 15:44 2024/8/1
     **/
    @GetMapping("/get")
    @Operation(summary = "获得客户管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:customer-main:query')")
    public CommonResult<CustomerMainRespVO> getCustomerMain(@RequestParam("id") String id) {
        return success(customerMainService.getCustomerById(id));
    }


    /**
     * 根据经办人所属企业查询经办人信息
     * @author PQZ
     * @date 16:15 2024/8/2
     * @param deptCode 经办人所属企业code
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<java.lang.Boolean>
     **/
    @GetMapping("/getAttn")
    @Operation(summary = "根据deptCode获取经办人信息")
    public CommonResult<List<CustomerMain>> getCustomerMainByDeptCode(@RequestParam("deptCode") String deptCode) {
        return success(customerMainService.getCustomerByDeptCode(deptCode));
    }

}