package com.njzscloud.dispose.cst.customer.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.njzscloud.common.core.utils.R;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.common.security.util.SecurityUtil;
import com.njzscloud.dispose.cst.customer.pojo.entity.CustomerEntity;
import com.njzscloud.dispose.cst.customer.pojo.param.AddCustomerParam;
import com.njzscloud.dispose.cst.customer.pojo.param.SearchCustomerParam;
import com.njzscloud.dispose.cst.customer.pojo.result.SearchCustomerResult;
import com.njzscloud.dispose.cst.customer.service.CustomerService;
import com.njzscloud.dispose.sys.auth.pojo.result.MyResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 客户信息
 */
@Slf4j
@RestController
@RequestMapping("/customer")
@RequiredArgsConstructor
public class CustomerController {
    private final CustomerService customerService;

    /**
     * 新增
     */
    @PostMapping("/add")
    public R<?> add(@RequestBody AddCustomerParam addCustomerParam) {
        customerService.add(addCustomerParam);
        return R.success();
    }

    /**
     * 查询当前用户已有的身份
     */
    @GetMapping("/get_user_identity")
    public R<?> getUserIdentity() {
        Long userId = SecurityUtil.currentUserId();
        return R.success(customerService.list(Wrappers.lambdaQuery(CustomerEntity.class).eq(CustomerEntity::getUserId, userId)));
    }

    /**
     * 切换身份
     *
     * @param targetCustomerId 目标身份
     */
    @GetMapping("/change_identity")
    public R<?> changeIdentity(@RequestParam("targetCustomerId") Long targetCustomerId) {
        MyResult myResult = SecurityUtil.loginUser();
        myResult = customerService.changeIdentity(targetCustomerId, myResult);
        return R.success(myResult);
    }

    /**
     * 修改
     */
    @PostMapping("/modify")
    public R<?> modify(@RequestBody CustomerEntity customerEntity) {
        customerService.modify(customerEntity);
        return R.success();
    }

    /**
     * 删除
     */
    @PostMapping("/del")
    public R<?> del(@RequestBody List<Long> ids) {
        customerService.del(ids);
        return R.success();
    }

    /**
     * 详情
     */
    @GetMapping("/detail")
    public R<SearchCustomerResult> detail(@RequestParam("id") Long id) {
        return R.success(customerService.detail(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/paging")
    public R<PageResult<SearchCustomerResult>> paging(PageParam pageParam, SearchCustomerParam searchCustomerParam) {
        return R.success(customerService.paging(pageParam, searchCustomerParam));
    }
}
