package com.crm.manager.controller;

import com.crm.manager.service.SysReasonsRefusalService;
import com.crm.rely.backend.core.constant.FeignClientAnnotation;
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.reasons.refusal.SysReasonsRefusalSearchDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.system.reasons.refusal.*;
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.Map;

/**
 * 拒绝理由
 *
 * @author houn
 */
@RestController
@RequestMapping("/reasons/refusal")
public class SysReasonsRefusalController {

    @Autowired
    private SysReasonsRefusalService sysReasonsRefusalService;

    /**
     * 添加拒绝理由
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    public BaseResultDto add(@RequestBody @Validated SysReasonsRefusalAddEntity entity) throws Exception {

        if (entity.getSubIndex() == null) {
            entity.setSubIndex(0);
        }
        sysReasonsRefusalService.add(entity);
        return BaseResultDto.success();
    }

    /**
     * 删除拒绝理由
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    public BaseResultDto delete(@RequestBody @Validated SysReasonsRefusalDeleteEntity entity) throws Exception {
        sysReasonsRefusalService.delete(entity);
        return BaseResultDto.success();
    }

    /**
     * 修改拒绝理由
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/update")
    public BaseResultDto update(@RequestBody @Validated SysReasonsRefusalUpdateEntity entity) throws Exception {
        if (entity.getSubIndex() == null) {
            entity.setSubIndex(0);
        }
        sysReasonsRefusalService.update(entity);
        return BaseResultDto.success();
    }

    /**
     * 列表查询
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/search/list")
    public BaseResultDto searchList(@RequestBody @Validated SysReasonsRefusalSearchPageEntity entity) throws Exception {


        ResultWithPagerDto<SysReasonsRefusalSearchDto> result = sysReasonsRefusalService.searchPageList(entity);

        return result;
    }

    /**
     * 单个查询
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/search/single")
    public BaseResultDto searchLingle(@RequestBody @Validated SingleLongEntity entity) throws Exception {

        SysReasonsRefusalSearchDto result = sysReasonsRefusalService.searchSingle(entity);

        return BaseResultDto.success(result);
    }

    @PostMapping("/list")
    public BaseResultDto list(@RequestBody @Validated SysReasonsRefusalSearchEntity entity) throws Exception {

        BaseResultDto<Map<String, SysReasonsRefusalSearchDto>> result = sysReasonsRefusalService.searchList(entity);

        return result;
    }

    @PostMapping("/custom/list")
    @FeignClientAnnotation
    public BaseResultDto<Map<String, SysReasonsRefusalSearchDto>> customList(@RequestBody @Validated SysReasonsRefusalSearchEntity entity) throws Exception {

        BaseResultDto<Map<String, SysReasonsRefusalSearchDto>> result = sysReasonsRefusalService.searchList(entity);

        return result;
    }
}
