package com.crm.manager.controller;

import com.crm.manager.service.SysPushMassageService;
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.push.massage.SysPushMassageSearchDto;
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.push.massage.*;
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("/push/message")
public class SysPushMassageController {

    @Autowired
    private SysPushMassageService sysPushMassageService;

    /**
     * 添加推送消息
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    public BaseResultDto add(@RequestBody @Validated SysPushMassageAddEntity entity) throws Exception {

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

    /**
     * 删除推送消息
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    public BaseResultDto delete(@RequestBody @Validated SysPushMassageDeleteEntity entity) throws Exception {
        sysPushMassageService.delete(entity);
        return BaseResultDto.success();
    }

    /**
     * 修改推送消息
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/update")
    public BaseResultDto update(@RequestBody @Validated SysPushMassageUpdateEntity entity) throws Exception {
        if (entity.getSubIndex() == null) {
            entity.setSubIndex(0);
        }
        sysPushMassageService.update(entity);
        return BaseResultDto.success();
    }

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


        ResultWithPagerDto<SysPushMassageSearchDto> result = sysPushMassageService.searchPageList(entity);

        return result;
    }

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

        SysPushMassageSearchDto result = sysPushMassageService.searchSingle(entity);

        return BaseResultDto.success(result);
    }

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

        BaseResultDto<Map<String, SysPushMassageSearchDto>> result = sysPushMassageService.searchList(entity);

        return result;
    }

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

        BaseResultDto<Map<String, SysPushMassageSearchDto>> result = sysPushMassageService.searchList(entity);

        return result;
    }
}
