package com.gic.haoban.manage.web.controller.test;

import com.gic.api.base.commons.Page;
import com.gic.haoban.base.api.common.BasePageInfo;
import com.gic.haoban.common.utils.HaobanResponse;
import com.gic.haoban.manage.web.controller.WebBaseController;
import com.gic.haoban.manage.web.errCode.HaoBanErrCode;
import com.gic.haoban.task.manage.api.dto.MarketClueTaskDTO;
import com.gic.haoban.task.manage.api.service.IMarketClueTaskApiService;
import com.gic.haoban.task.manage.api.service.IMarketTaskMemberApiService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

import static org.slf4j.LoggerFactory.getLogger;

/**
 * Created 2021/6/21.
 *
 * @author hua
 */
@RestController
@RequestMapping("/test-data")
public class TestDataImportController extends WebBaseController {

    private static final Logger logger = getLogger(TestDataImportController.class);

    @Autowired
    private IMarketClueTaskApiService iMarketClueTaskApiService;

    @Autowired
    private IMarketTaskMemberApiService iMarketTaskMemberApiService;
   

    /**
     * 处理任务
     *
     * @param ecmId           计划id
     * @param enterpriseId    企业id
     * @param touchFinishType 完成类型 任务完成触达方式, 1 : 企业微信, 2 : 企业电话, 3 : 私人电话, 4 : 私人短信',
     * @param rate            0-100  完成百分比
     * @return
     */
    @RequestMapping("/deal-task")
    public HaobanResponse dealTask(String ecmId, String enterpriseId, String touchFinishType, int rate) {
        if (StringUtils.isAnyBlank(ecmId, enterpriseId, touchFinishType)) {
            return resultResponse(HaoBanErrCode.ERR_2);
        }
        if (rate <= 0) {
            logger.info("rate大于0需要");
            return resultResponse(HaoBanErrCode.ERR_1);
        }
        BasePageInfo basePageInfo = new BasePageInfo();
        basePageInfo.setPageSize(2000);
        int num = 1;
        Page<MarketClueTaskDTO> page = iMarketClueTaskApiService.pageMarketClueTaskDTO(ecmId, enterpriseId, basePageInfo);
        while (CollectionUtils.isNotEmpty(page.getResult())) {
            page.getResult().forEach(dto -> {
                Random random = new Random(100);
                int i = random.nextInt();
                if (i <= rate) {
                    logger.info("处理：{}", com.alibaba.fastjson.JSONObject.toJSONString(dto));
                    iMarketTaskMemberApiService.detailsRefresh(enterpriseId, dto.getTaskId());
                }
            });
            num++;
            basePageInfo.setPageNum(num);
            page = iMarketClueTaskApiService.pageMarketClueTaskDTO(ecmId, enterpriseId, basePageInfo);
        }
        return resultResponse(HaoBanErrCode.ERR_1);
    }
}
