package com.pj.project.tb_message;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.pj.api.dto.MessageDto;
import com.pj.project4sp.SP;
import com.pj.utils.sg.AjaxJson;
import com.pj.utils.so.SoMap;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;


/**
 * Controller: tb_message -- 通知消息
 *
 * @author lsw
 */
@RestController
@RequestMapping("/TbMessage/")
public class TbMessageController {

    /**
     * 底层 Service 对象
     */
    @Autowired
    TbMessageService tbMessageService;

    /**
     * 增
     */
    @RequestMapping("add")
    @SaCheckPermission(TbMessage.PERMISSION_CODE_ADD)
    public AjaxJson add(TbMessage t) {
        t.setTypes(1);
        tbMessageService.add(t);
        return AjaxJson.getSuccess();
    }

    /**
     * 删
     */
    @RequestMapping("delete")
    @SaCheckPermission(TbMessage.PERMISSION_CODE_DEL)
    public AjaxJson delete(Long id) {
        tbMessageService.delete(id);
        return AjaxJson.getSuccess();
    }

    /**
     * 删 - 根据id列表
     */
    @RequestMapping("deleteByIds")
    @SaCheckPermission(TbMessage.PERMISSION_CODE_DEL)
    public AjaxJson deleteByIds() {
        List<Long> ids = SoMap.getRequestSoMap().getListByComma("ids", long.class);
        int line = SP.publicMapper.deleteByIds(TbMessage.TABLE_NAME, ids);
        return AjaxJson.getByLine(line);
    }

    /**
     * 改
     */
    @RequestMapping("update")
    @SaCheckPermission(TbMessage.PERMISSION_CODE_EDIT)
    public AjaxJson update(TbMessage t) {
        tbMessageService.update(t);
        return AjaxJson.getSuccess();
    }

    /**
     * 查 - 根据id
     */
    @RequestMapping("getById")
    @SaCheckPermission(TbMessage.PERMISSION_CODE)
    public AjaxJson getById(Integer id) {
        TbMessage t = tbMessageService.getById(id);
        return AjaxJson.getSuccessData(t);
    }

    /**
     * 查集合 - 根据条件（参数为空时代表忽略指定条件）
     */
    @RequestMapping("getList")
    @SaCheckPermission(TbMessage.PERMISSION_CODE)
    public AjaxJson getList() {
        SoMap so = SoMap.getRequestSoMap();
        so.set("types", 1);
        List<TbMessage> list = tbMessageService.getList(so.startPage());
        return AjaxJson.getPageData(so.getDataCount(), list);
    }

    @RequestMapping(value = "rpc/save")
    public AjaxJson save(@RequestBody MessageDto dto) {
        TbMessage message = new TbMessage();
        BeanUtils.copyProperties(dto, message);
        message.setEnable(1);
        message.setIsRead(0);
        message.setTypes(2);
        message.setCreateTime(new Date());
        tbMessageService.save(message);
        return AjaxJson.getSuccess();
    }

}
