package com.dxhy.extend.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dxhy.common.controller.AbstractController;
import com.dxhy.common.utils.R;
import com.dxhy.extend.entity.ExtSysMessagecontrol;
import com.dxhy.extend.service.messagecontrol.ExtMessageControlService;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;

/**
 *
 * @ClassName: MessageControlController :
 * @author: xiexuedi
 * @date: 2019年4月22日 下午13:32:43
 *
 */
@SuppressWarnings("AlibabaUndefineMagicConstant")
@RestController
@RequestMapping("message")
@Slf4j
public class ExtMessageControlController extends AbstractController {
    @Resource
    private ExtMessageControlService messageControlService;

    @RequestMapping("/queryMessageContol")
    public ResponseEntity<R> queryMessageContol(@RequestBody Map<String, Object> pramsMap) {
        String operationStatus = (String)pramsMap.get("operationStatus");
        Integer curr = (Integer)pramsMap.get("curr");
        Integer size = (Integer)pramsMap.get("size");
        String userId = getUserName();
        if ("99".equals(operationStatus)) {
            operationStatus = "";
        }
        Object object = messageControlService.queryMessageContol(userId, curr, size, operationStatus);
        return ResponseEntity.ok(R.ok().put("data", object));
    }

    @RequestMapping("/queryCount")
    public ResponseEntity<R> queryCount() {
        String userId = getUserName();
        Integer count = messageControlService.queryCount(userId);
        return ResponseEntity.ok(R.ok().put("data", count));
    }

    @RequestMapping("/updateOperationStatus")
    public ResponseEntity<R> updateOperationStatus(@RequestBody Map<String, Object> pramsMap) {
        ExtSysMessagecontrol vo = new ExtSysMessagecontrol();
        String type = (String)pramsMap.get("type");
        vo.setUpdateTime(DateUtil.date());
        if ("1".equals(type)) {
            Long id = Long.parseLong((String)pramsMap.get("id"));
            vo.setId(id);
            int count = messageControlService.updateOperationStatusById(vo);
            return ResponseEntity.ok(R.ok().put("data", count));
        } else {
            vo.setUserAccount(getUserName());
            int count = messageControlService.updateOperationStatusAll(vo);
            return ResponseEntity.ok(R.ok().put("data", count));
        }
    }

    @RequestMapping("/addmessage")
    public void addmessage(@RequestBody ExtSysMessagecontrol message) {
        ExtSysMessagecontrol vo = new ExtSysMessagecontrol();
        vo.setTitle(message.getTitle());
        vo.setContent(message.getContent());
        vo.setUserAccount(message.getUserAccount());
        Date time2 = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String time = sdf.format(new Date());
            time2 = sdf.parse(time);
        } catch (ParseException e) {
           log.info("业务处理异常：{}", e);
        }
        vo.setCreateTime(time2);
        vo.setUpdateTime(time2);
        vo.setUrl(message.getUrl());
        vo.setOperationStatus("0");
        messageControlService.insertMessage(vo);
    }
}
