package com.crm.manager.controller;

import com.crm.manager.service.SysSourceService;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
import com.crm.rely.backend.core.dto.system.source.SysSourceDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.system.source.SysSourceAddEntity;
import com.crm.rely.backend.core.entity.system.source.SysSourceDeleteEntity;
import com.crm.rely.backend.core.entity.system.source.SysSourceSearchPageEntity;
import com.crm.rely.backend.core.entity.system.source.SysSourceUpdateEntity;
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;

/**
 * @author: houn
 */
@RestController
@RequestMapping("/source")
public class SysSourceController {

    @Autowired
    private SysSourceService service;

    /**
     * 添加
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    public BaseResultDto add(@RequestBody @Validated SysSourceAddEntity entity) throws Exception {
        service.add(entity);
        return BaseResultDto.success();
    }

    /**
     * 删除
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    public BaseResultDto delete(@RequestBody @Validated SysSourceDeleteEntity entity) throws Exception {

        service.delete(entity);
        return BaseResultDto.success();

    }

    /**
     * 修改
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/update")
    public BaseResultDto update(@RequestBody @Validated SysSourceUpdateEntity entity) throws Exception {

        service.update(entity);
        return BaseResultDto.success();

    }

    /**
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/search/list")
    public BaseResultDto list(@RequestBody @Validated SysSourceSearchPageEntity entity) throws Exception {

        ResultWithPagerDto<SysSourceDto> sysSourceDtoResultWithPagerDto = service.pageSearchList(entity);
        return sysSourceDtoResultWithPagerDto;

    }

    @PostMapping("/search/single")
    public BaseResultDto single(@RequestBody @Validated SingleLongEntity entity) throws Exception {
        SysSourceDto dto = service.searchSingle(entity.getId());
        return BaseResultDto.success(dto);

    }

    @PostMapping("/list")
    public BaseResultDto list() throws Exception {
        List<SysSourceDto> dtos = service.getAllSource();
        return BaseResultDto.success(dtos);

    }
}
