package com.crm.manager.controller;

import com.crm.login.rely.backend.controller.BaseLoginController;
import com.crm.manager.service.TenantService;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.BaseResultWithPagerDto;
import com.crm.rely.backend.core.entity.tenant.info.TenantAddEntity;
import com.crm.rely.backend.core.entity.tenant.info.TenantEditEntity;
import com.crm.rely.backend.core.entity.tenant.info.TenantEntity;
import com.crm.rely.backend.core.entity.tenant.info.TenantSearchEntity;
import com.crm.rely.backend.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商家管理
 */
@RestController
@RequestMapping("/merchant")
public class MerchantController extends BaseLoginController {

    @Autowired
    private TenantService tenantService;

    /**
     * 商户列表
     * @return
     */
    @PostMapping("/search/list")
    public BaseResultDto searchPage(@RequestBody TenantSearchEntity entity) throws ServiceException {
        BaseResultWithPagerDto resultWithPagerDto = tenantService.searchPage(entity);
        return resultWithPagerDto;
    }

    /**
     * 添加商户
     * @param entity
     * @return
     */
    @PostMapping("/add")
    public BaseResultDto add(@RequestBody TenantAddEntity entity) {
        return BaseResultDto.success(tenantService.add(entity));
    }

    /**
     * 根据 id 查询指定商户
     * @param id
     * @return
     */
    @PostMapping("/findSingle")
    public BaseResultDto findSingle(@RequestParam Long id) {
        return BaseResultDto.success(tenantService.findSingle(id));
    }

    /**
     * 修改商户
     * @param entity
     * @return
     */
    @PostMapping("/edit")
    public BaseResultDto edit(@RequestBody TenantEditEntity entity) {
        return BaseResultDto.success(tenantService.edit(entity));
    }

    /**
     * 删除商户
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public BaseResultDto delete(@RequestParam Long id) {
        return BaseResultDto.success(tenantService.delete(id));
    }

    /**
     * 获取所有商户
     * @return
     */
    @PostMapping("/getAllMerchant")
    public BaseResultDto<List<TenantEntity>> getAllMerchant() {
        return BaseResultDto.success(tenantService.getAllMerchant());
    }
}
