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.CustomerServiceReceiveVo;
import com.yonge.cooleshow.biz.dal.entity.CustomerServiceReceive;
import com.yonge.cooleshow.biz.dal.service.CustomerServiceReceiveService;
import com.yonge.cooleshow.biz.dal.wrapper.im.CustomerServiceReceiveWrapper;
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.validation.annotation.Validated;
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.List;
import java.util.Objects;

@Slf4j
@Validated
@RestController
@RequestMapping("${app-config.url.admin:}/batchReceive")
@Api(tags = "客服群发接收")
public class CustomerServiceReceiveController {

    @Autowired
    private CustomerServiceReceiveService customerServiceReceiveService;

    
    /**
	 * 查询分页
	 * @param query CustomerServiceReceiveVo.CustomerServiceReceiveQuery
	 * @return R<PageInfo<CustomerServiceReceiveVo.CustomerServiceReceive>>
	 */
    @ApiOperation(value = "查询分页", notes = "客服群发接收- 传入 CustomerServiceReceiveVo.CustomerServiceReceiveQuery") 
    @PostMapping("/page")
    public HttpResponseResult<PageInfo<CustomerServiceReceiveWrapper.CustomerServiceReceive>> page(@RequestBody CustomerServiceReceiveWrapper.CustomerServiceReceiveQuery query) {
    
        // 查询数据
        IPage<CustomerServiceReceiveWrapper.CustomerServiceReceive> pages = customerServiceReceiveService.selectPage(PageUtil.getPage(query), query);

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

		if (CollectionUtils.isEmpty(info)
				|| info.stream().anyMatch(x -> Objects.isNull(x.getBatchSendingId()))) {
			throw new BizException("无效的请求参数");
		}

		List<CustomerServiceReceive> receives = JSON.parseArray(JSON.toJSONString(info), CustomerServiceReceive.class);
		// 新增数据
        customerServiceReceiveService.saveBatch(receives);
        
        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(customerServiceReceiveService.removeById(id));
	}
}