/**
 *
 */
package com.sifang.modules.taskdispatch.controller;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.utils.BusinessException;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseController;
import com.sifang.modules.taskdispatch.service.CrmReviewDispatchService;
import com.sifang.modules.taskdispatch.vo.ItemAssignReq;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 待办派工Controller
 *
 * @author gaols
 */
@Controller
@RequestMapping(value = "${adminPath}/taskdispatch/crmReviewDispatch")
public class CrmReviewDispatchController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(CrmReviewDispatchController.class);

    @Autowired
    private CrmReviewDispatchService crmReviewDispatchService;

    @RequestMapping(value = {"listData"})
    @ResponseBody
    public String listData(HttpServletRequest request) {
        LayuiPage<Record> page = new LayuiPage<>(request);
        Map<String, String> params = getParams(request);
        crmReviewDispatchService.listData(page, params);
        return page.renderJson();
    }

    @RequestMapping("batchAssign")
    @ResponseBody
    public Result batchAssign(@RequestBody ItemAssignReq req) {
        try {
            crmReviewDispatchService.batchAssign(req);
            return Result.ok();
        } catch (BusinessException ex) {
            log.error("batch assign error", ex);
            return Result.fail(ex.getMessage());
        } catch (Exception ex) {
            log.error("batch assign error", ex);
            return Result.fail("系统内部错误！");
        }
    }

    @RequestMapping("batchReAssign")
    @ResponseBody
    public Result batchReAssign(@RequestBody ItemAssignReq req) {
        try {
            crmReviewDispatchService.batchReAssign(req);
            return Result.ok();
        } catch (BusinessException ex) {
            log.error("batch redispatch error", ex);
            return Result.fail(ex.getMessage());
        } catch (Exception ex) {
            log.error("batch redispatch error", ex);
            return Result.fail("系统内部错误！");
        }
    }

    @RequestMapping(value = {"batchAssignPage"})
    public String batchAssignPage(String groupId, Model model) {
        return "modules/yangguang/systemSetting/flowable/item/projreviewGroupEditPage";
    }

}
