package com.yonge.cooleshow.admin.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yonge.cooleshow.admin.io.request.im.CustomerServiceBatchSendingVo;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.enums.im.EImReceiveType;
import com.yonge.cooleshow.biz.dal.enums.im.EImSendStatus;
import com.yonge.cooleshow.biz.dal.service.CustomerServiceBatchSendingService;
import com.yonge.cooleshow.biz.dal.wrapper.im.CustomerServiceBatchSendingWrapper;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.base.exception.BizException;
import com.yonge.toolset.base.page.PageInfo;
import com.yonge.toolset.mybatis.support.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@Slf4j
@Validated
@RestController
@RequestMapping("${app-config.url.admin:}/batchSending")
@Api(tags = "客服群发")
public class CustomerServiceBatchSendingController extends BaseController {

    @Autowired
    private CustomerServiceBatchSendingService customerServiceBatchSendingService;
	@Autowired
	private SysUserFeignService sysUserFeignService;

	/**
	 * 查询单条
	 * @param id 详情ID
	 * @return R<CustomerServiceBatchSendingVo.CustomerServiceBatchSending>
	 */
	@ApiOperation(value = "详情", notes = "客服群发-根据详情ID查询单条, 传入id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "long")
    })
    @GetMapping("/detail/{id}")
    public HttpResponseResult<CustomerServiceBatchSendingWrapper.CustomerServiceBatchSending> detail(@PathVariable("id") Long id) {

		CustomerServiceBatchSendingWrapper.CustomerServiceBatchSending wrapper = customerServiceBatchSendingService.detail(id);
        
        return HttpResponseResult.succeed(wrapper);
	}
    
    /**
	 * 查询分页
	 * @param query CustomerServiceBatchSendingVo.CustomerServiceBatchSendingQuery
	 * @return R<PageInfo<CustomerServiceBatchSendingVo.CustomerServiceBatchSending>>
	 */
    @ApiOperation(value = "查询分页", notes = "客服群发- 传入 CustomerServiceBatchSendingVo.CustomerServiceBatchSendingQuery") 
    @PostMapping("/page")
    public HttpResponseResult<PageInfo<CustomerServiceBatchSendingWrapper.CustomerServiceBatchSending>> page(@RequestBody CustomerServiceBatchSendingWrapper.CustomerServiceBatchSendingQuery query) {
    
        // 查询数据
        IPage<CustomerServiceBatchSendingWrapper.CustomerServiceBatchSending> pages = customerServiceBatchSendingService.selectPage(PageUtil.getPage(query), query);

        return HttpResponseResult.succeed(PageUtil.pageInfo(pages));
	}
    
    /**
	 * 新增
	 * @param info CustomerServiceBatchSendingVo.CustomerServiceBatchSending
	 * @return R<Boolean>
	 */
    @ApiOperation(value = "新增", notes = "客服群发- 传入 CustomerServiceBatchSendingVo.CustomerServiceBatchSending")
	@PostMapping("/save")
	public HttpResponseResult<Boolean> add(@RequestBody CustomerServiceBatchSendingVo.CustomerServiceBatchSending info) {

		// 获取当前用户ID
		SysUser user = sysUserFeignService.queryUserInfo();
		if (user == null || null == user.getId()) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}
		info.setCreateBy(user.getId());

		if (EImReceiveType.PORTION == info.getReceiveType()
				&& CollectionUtils.isEmpty(info.getReceives())) {
			throw new BizException("接收用户不能为空");
		}
		// 设置默认参数
		info.setSendStatus(EImSendStatus.WAIT);

        // 新增数据
        customerServiceBatchSendingService.add(JSON.parseObject(info.jsonString(),
				CustomerServiceBatchSendingWrapper.CustomerServiceBatchSending.class));
        
        return HttpResponseResult.succeed();
	}
    
    /**
	 * 修改
	 * @param info CustomerServiceBatchSendingVo.CustomerServiceBatchSending
	 * @return R<Boolean>
	 */
    @ApiOperation(value = "修改", notes = "客服群发- 传入 CustomerServiceBatchSendingVo.CustomerServiceBatchSending")
	@PostMapping("/update")
	public HttpResponseResult<Boolean> update(@Validated @RequestBody CustomerServiceBatchSendingVo.CustomerServiceBatchSending info) {

		if (Objects.isNull(info.getId())) {
			throw new BizException("请求参数异常");
		}

        // 更新数据
        customerServiceBatchSendingService.update(JSON.parseObject(info.jsonString(),
				CustomerServiceBatchSendingWrapper.CustomerServiceBatchSending.class));
        
        return HttpResponseResult.succeed();
	}

 	/**
	 * 删除
	 * @param id 详情ID
	 * @return R<Boolean>
	 */
	@ApiOperation(value = "删除", notes = "客服群发- 传入id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "long")
    })
	@PostMapping("/remove")
	public HttpResponseResult<Boolean> remove(@RequestParam Long id) {
    
		return HttpResponseResult.succeed(customerServiceBatchSendingService.removeById(id));
	}

	@ApiOperation(value = "更新消息状态", notes = "客服群发- 传入id")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "主键Id", dataType = "long"),
			@ApiImplicitParam(name = "sendStatus", value = "发送状态", dataType = "String")
	})
	@PostMapping("/status/{id}")
	public HttpResponseResult<Boolean> status(@PathVariable("id") Long id, EImSendStatus sendStatus) {

		if (Objects.isNull(sendStatus)) {
			throw new BizException("发送状态不能为空");
		}

		// 更新群发消息状态
		customerServiceBatchSendingService.status(id, sendStatus);

		return HttpResponseResult.succeed(true);
	}

	@ApiOperation(value = "立即发送", notes = "客服群发- 传入id")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "主键Id", dataType = "long")
	})
	@PostMapping("/send/{id}")
	public HttpResponseResult<Boolean> send(@PathVariable("id") Long id) {

		// 更新群发消息状态
		customerServiceBatchSendingService.sendMessage(id);

		return HttpResponseResult.succeed(true);
	}
}