package com.lz.modules.third.controller;


import com.alibaba.fastjson.JSONObject;
import com.lz.common.utils.PageUtils;
import com.lz.common.utils.R;
import com.lz.common.utils.StringUtil;
import com.lz.modules.job.business.DingtalkBusiness;
import com.lz.modules.third.entity.ThirdMsgSendRecord;
import com.lz.modules.third.service.ThirdMsgSendRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/thirdMsgSendRecord")
public class ThirdMsgSendRecordController {


    @Autowired
    private ThirdMsgSendRecordService thirdMsgSendRecordservice;

    @Autowired
    private DingtalkBusiness dingtalkBusiness;
    @Value("${dingtalk.appid}")
    private String appid;

    @RequestMapping("/list")
     public R list(@RequestBody String body) {
        Map<String,Object> params = new HashMap<>();
        if(StringUtil.isNotBlank(body)){
            params = JSONObject.parseObject(body,Map.class);
        }
        PageUtils page = thirdMsgSendRecordservice.queryPage(params);
        return R.ok().put("page", page);
     }

    @GetMapping("/asyn/send/work/msg")
    public R asynSendWorkMsg(@RequestParam Long fromstaffid, @RequestParam Long tostaffid, @RequestParam int type) {

         String msg = dingtalkBusiness.sendWorkMSG(appid, fromstaffid, tostaffid, type);
         if(msg.equals("OK")){
             return R.ok();
         }else{
             return R.error(msg);
         }
    }


     @RequestMapping("/getById")
     public R getById(@RequestBody ThirdMsgSendRecord thirdMsgSendRecord) {
        thirdMsgSendRecord = thirdMsgSendRecordservice.selectThirdMsgSendRecordById(thirdMsgSendRecord.getId());
        return R.ok().put("thirdMsgSendRecord",thirdMsgSendRecord);
     }


     @RequestMapping("/update")
     public R update(@RequestBody ThirdMsgSendRecord thirdMsgSendRecord) {
        thirdMsgSendRecordservice.updateThirdMsgSendRecordById(thirdMsgSendRecord);
        return R.ok();
     }


     @RequestMapping("/save")
        public R save(@RequestBody ThirdMsgSendRecord thirdMsgSendRecord) {
        thirdMsgSendRecordservice.insertThirdMsgSendRecord(thirdMsgSendRecord);
        return R.ok();
     }


    @RequestMapping("/delete")
    public R list(@RequestBody Long id) {
        thirdMsgSendRecordservice.deleteThirdMsgSendRecordById(id);
        return R.ok();
     }
}
