package com.crm.manager.controller;

import com.crm.manager.service.SalesCommissionRuleService;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.BaseResultWithPagerDto;
import com.crm.rely.backend.core.dto.sales.SalesCommissionRuleDropdownDto;
import com.crm.rely.backend.core.dto.sales.SalesCommissionRuleDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.login.InfoEntity;
import com.crm.rely.backend.core.entity.sales.SalesCommissionRuleAddEntity;
import com.crm.rely.backend.core.entity.sales.SalesCommissionRuleSearchEntity;
import com.crm.rely.backend.core.entity.sales.SalesCommissionRuleUpdateEntity;
import com.crm.rely.backend.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/sales/commission/rule")
public class SalesCommissionRuleController {

    @Autowired
    private SalesCommissionRuleService salesCommissionRuleService;

    @PostMapping("/add")
    public BaseResultDto<Object> add(@RequestBody SalesCommissionRuleAddEntity entity, InfoEntity infoEntity) throws ServiceException{
        salesCommissionRuleService.save(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/update")
    public BaseResultDto<Object> update(@RequestBody SalesCommissionRuleUpdateEntity entity, InfoEntity infoEntity) throws ServiceException{
        salesCommissionRuleService.updateById(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/deleteById")
    public BaseResultDto<Object> deleteById(@RequestBody SingleLongEntity entity, InfoEntity infoEntity) throws ServiceException{
        salesCommissionRuleService.deleteById(entity.getId());
        return BaseResultDto.success();
    }

    @PostMapping("/search/single")
    public BaseResultDto<SalesCommissionRuleDto> searchSingle(@RequestBody SingleLongEntity entity) throws ServiceException{
        return BaseResultDto.success(salesCommissionRuleService.getById(entity.getId()));
    }

    @PostMapping("/search/list")
    public BaseResultWithPagerDto<List<SalesCommissionRuleDto>> searchList(@RequestBody SalesCommissionRuleSearchEntity   entity) throws ServiceException{
        return salesCommissionRuleService.searchList(entity);
    }

    @PostMapping("/search/dropdown")
    public BaseResultDto<List<SalesCommissionRuleDropdownDto>> searchDropdown() throws ServiceException{
        return BaseResultDto.success(salesCommissionRuleService.searchDropdownList());
    }

}
