package com.crm.manager.controller;

import com.crm.manager.service.SalesPerformanceConfigService;
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.SalesPerformanceConfigDto;
import com.crm.rely.backend.core.dto.sales.SalesPerformanceSalesDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.custom.info.InfoEntity;
import com.crm.rely.backend.core.entity.sales.SalesPerformanceConfigAddEntity;
import com.crm.rely.backend.core.entity.sales.SalesPerformanceConfigSearchEntity;
import com.crm.rely.backend.core.entity.sales.SalesPerformanceConfigUpdateEntity;
import com.crm.rely.backend.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
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/performance/config")
public class SalesPerformanceConfigController {

    @Autowired
    private SalesPerformanceConfigService salesPerformanceConfigService;

    @PostMapping("/add")
    public BaseResultDto<Boolean> add(@RequestBody @Validated SalesPerformanceConfigAddEntity entity, InfoEntity infoEntity) throws ServiceException {
        salesPerformanceConfigService.saveByEntity(entity, infoEntity);
        return BaseResultDto.success();
    }


    @PostMapping("/update")
    public BaseResultDto<Boolean> update(@RequestBody @Validated SalesPerformanceConfigUpdateEntity entity, InfoEntity infoEntity) throws ServiceException {
        salesPerformanceConfigService.modifyByEntity(entity, infoEntity);
        return BaseResultDto.success();
    }

    @PostMapping("/delete")
    public BaseResultDto<Boolean> delete(@RequestBody @Validated SingleLongEntity entity, InfoEntity infoEntity) throws ServiceException {
        salesPerformanceConfigService.removeById(entity.getId(), infoEntity);
        return BaseResultDto.success();
    }

    @PostMapping("/search/single")
    public BaseResultDto<SalesPerformanceConfigDto> searchSingle(@RequestBody @Validated SingleLongEntity entity, InfoEntity infoEntity) throws ServiceException {
        return BaseResultDto.success(salesPerformanceConfigService.searchById(entity.getId(), infoEntity));
    }

    @PostMapping("/search/list")
    public BaseResultWithPagerDto<List<SalesPerformanceConfigDto>> searchList(@RequestBody SalesPerformanceConfigSearchEntity entity, InfoEntity infoEntity) throws ServiceException {
        entity.setStamp(infoEntity.getIbInfo().getStamp());
        return salesPerformanceConfigService.searchList(entity);
    }

    @PostMapping("/sales")
    public BaseResultDto<List<SalesPerformanceSalesDto>> sales(InfoEntity infoEntity) throws ServiceException {
        return BaseResultDto.success(salesPerformanceConfigService.getSalesDropdown(infoEntity));
    }

}
