package com.jpsoft.shinestar.modules.base.controller;

import com.github.pagehelper.Page;
import com.jpsoft.shinestar.modules.common.utils.PojoUtils;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.jpsoft.shinestar.modules.common.dto.MessageResult;
import com.jpsoft.shinestar.modules.base.entity.MessageReceiver;
import com.jpsoft.shinestar.modules.base.service.MessageReceiverService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/base/messageReceiver")
@Api(description = "messageReceiver")
public class MessageReceiverController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private MessageReceiverService messageReceiverService;

    @ApiOperation(value="获取信息")
    @GetMapping("edit/{id}")
    public MessageResult<MessageReceiver> edit(@PathVariable("id") String id){
        MessageResult<MessageReceiver> msgResult = new MessageResult<>();

        try {
            MessageReceiver messageReceiver = messageReceiverService.get(id);

            if (messageReceiver != null) {
                msgResult.setResult(true);
                msgResult.setData(messageReceiver);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("数据库不存在该记录!");
            }
        }
        catch(Exception ex){
            logger.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }

    @ApiOperation(value="更新用户")
    @PostMapping("update")
    public MessageResult<MessageReceiver> update(@RequestBody MessageReceiver messageReceiver,@RequestAttribute String subject){
        MessageResult<MessageReceiver> msgResult = new MessageResult<>();

        try {
            messageReceiver.setUpdateBy(subject);
            messageReceiver.setUpdateTime(new Date());
            
            int affectCount = messageReceiverService.update(messageReceiver);

            if (affectCount > 0) {
                msgResult.setResult(true);
                msgResult.setData(messageReceiver);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("数据库更新失败");
            }
        }
        catch(Exception ex){
            logger.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }

	@ApiOperation(value="删除")
    @PostMapping("delete/{id}")
    public MessageResult<Integer> delete(@PathVariable("id") String id,@RequestAttribute String subject){
        MessageResult<Integer> msgResult = new MessageResult<>();

        try {
            MessageReceiver messageReceiver = messageReceiverService.get(id);
            messageReceiver.setDelFlag(true);
            messageReceiver.setUpdateBy(subject);
            messageReceiver.setUpdateTime(new Date());

            int affectCount = messageReceiverService.update(messageReceiver);

            if (affectCount > 0) {
                msgResult.setResult(true);
                msgResult.setData(affectCount);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("删除失败");
            }
        }
        catch(Exception ex){
            logger.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }

    @ApiOperation(value="批量删除")
    @PostMapping("batchDelete")
    public MessageResult<Integer> batchDelete(@RequestBody List<String> idList,@RequestAttribute String subject){
        MessageResult<Integer> msgResult = new MessageResult<>();

        try {
            int affectCount = 0;

            for (String id : idList) {
                MessageReceiver messageReceiver = messageReceiverService.get(id);
                messageReceiver.setDelFlag(true);
                messageReceiver.setUpdateBy(subject);
                messageReceiver.setUpdateTime(new Date());

                affectCount += messageReceiverService.update(messageReceiver);
            }

            if (affectCount > 0) {
                msgResult.setResult(true);
                msgResult.setData(affectCount);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("删除失败");
            }
        }
        catch(Exception ex){
            logger.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }

    @ApiOperation(value="列表")
    @RequestMapping(value = "pageList",method = RequestMethod.POST)
    public MessageResult<Map> pageList(
            String messageId,
            String companyId,
            String companyStructureId,
            String roleId,
            String personName,
            Boolean readStatus,
            @RequestParam(value="pageIndex",defaultValue="1") int pageIndex,
            @RequestParam(value="pageSize",defaultValue="20") int pageSize,
            @RequestAttribute String subject){
        MessageResult<Map> msgResult = new MessageResult<>();

        Map<String,Object> searchParams = new HashMap<>();

        List<Sort> sortList = new ArrayList<>();
        sortList.add(new Sort("create_time","asc"));

        searchParams.put("messageId", messageId);

        if (StringUtils.isNotEmpty(companyId)) {
            searchParams.put("companyId",companyId);
        }

        if (StringUtils.isNotEmpty(companyStructureId)) {
            searchParams.put("companyStructureId", companyStructureId);
        }

        if (StringUtils.isNotEmpty(roleId)) {
            searchParams.put("roleId", roleId);
        }

        if (StringUtils.isNotEmpty(personName)) {
            searchParams.put("personName","%" + personName + "%");
        }

        if (readStatus!=null){
            searchParams.put("readStatus", readStatus);
        }

        Page<MessageReceiver> page = messageReceiverService.pageSearch(searchParams,pageIndex,pageSize,true,sortList);

        msgResult.setResult(true);
        msgResult.setData(PojoUtils.pageWrapper(page));

        return msgResult;
    }
}
