package com.ho.flow.controller;

import com.ho.common.tools.constant.ContextConstant;
import com.ho.common.tools.constant.RedisKeyConstant;
import com.ho.common.tools.entity.SimpleUser;
import com.ho.common.tools.exception.BaseResponseCode;
import com.ho.common.tools.exception.BusinessException;
import com.ho.common.tools.exception.DataResult;
import com.ho.common.tools.service.RedisService;
import com.ho.common.tools.util.PageResult;
import com.ho.flow.entity.SendSmsConfig;
import com.ho.flow.service.SendSmsConfigService;
import com.ho.flow.vo.req.SendSmsConfig.SendSmsConfigDeleteReq;
import com.ho.flow.vo.req.SendSmsConfig.SendSmsConfigQueryReq;
import com.ho.flow.vo.resp.SendSmsConfig.SendSmsConfigRsp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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 javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 发送短信配置界面
 */
@RestController
@RequestMapping(ContextConstant.FLOW_CONTEXT + "sendSmsConfig")
@Api(tags = "短信配置人员界面")
@Slf4j
public class SendSmsConfigController {

    @Autowired
    SendSmsConfigService sendSmsConfigService;

    @Autowired
    RedisService redisService;

    @PostMapping(value = "page")
    @ApiOperation("分页查询短信配置信息")
    public DataResult page(@RequestBody SendSmsConfigQueryReq vo){
        PageResult<SendSmsConfigRsp> pageList = sendSmsConfigService.getPageList(vo);
        return DataResult.success(pageList);
    }

    @PostMapping(value = "insertList")
    @ApiOperation("批量插入")
    public DataResult insertList(@RequestBody List<SendSmsConfig> list, HttpServletRequest request){
        if(!list.isEmpty()){
            List<String> useIds = list.stream().map(SendSmsConfig::getUserId).collect(Collectors.toList());
            SendSmsConfigQueryReq vo = new SendSmsConfigQueryReq();
            vo.setStationId(list.get(0).getStationId());
            vo.setSmsType(list.get(0).getSmsType());
            vo.setUserIds(useIds);
            List<SendSmsConfigRsp> queryList = sendSmsConfigService.getList(vo);
            if(!queryList.isEmpty()){
                throw new BusinessException(BaseResponseCode.EXISTS_DUPLICATE_DATA);
            }
            String token = request.getHeader(RedisKeyConstant.User.ACCESS_TOKEN);
            SimpleUser user = redisService.getSimpleUserByToken(token);
            list.stream().forEach(i->i.setCreateId(user.getUserId()));
            sendSmsConfigService.insertList(list);
        }
        return DataResult.success();
    }

    @PostMapping(value = "updateById")
    @ApiOperation("更新")
    public DataResult updateById(@RequestBody SendSmsConfig vo, HttpServletRequest request){
        String token = request.getHeader(RedisKeyConstant.User.ACCESS_TOKEN);
        SimpleUser user = redisService.getSimpleUserByToken(token);
        vo.setUpdateId(user.getUserId());
        int record = sendSmsConfigService.updateById(vo);
        return DataResult.success();
    }

    @PostMapping(value = "deleteList")
    @ApiOperation("批量删除")
    public DataResult updateById(@RequestBody SendSmsConfigDeleteReq vo){
        int record = sendSmsConfigService.deleteList(vo.getIds());
        return DataResult.success();
    }
}
