package com.uas.platform.b2b.manage.controller;

import com.uas.platform.b2b.manage.core.util.StringUtils;
import com.uas.platform.b2b.manage.model.UasSchedular;
import com.uas.platform.b2b.manage.model.UseType;
import com.uas.platform.b2b.manage.page.PageInfo;
import com.uas.platform.b2b.manage.page.PageParams;
import com.uas.platform.b2b.manage.service.UasSchedularService;
import com.uas.platform.b2b.manage.service.UseLogService;
import com.uas.platform.b2b.manage.web.BaseController;
import com.uas.platform.core.util.AgentUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * ERP轮询地址
 *
 * @author hejq
 * @date 2018-07-04 18:32
 */
@RestController
@RequestMapping("/schedular")
public class UasSchedularController extends BaseController {

    @Autowired
    private UasSchedularService schedularService;

    @Autowired
    private UseLogService useLogService;

    /**
     * 设置轮询地址
     *
     * @param schedular 轮询信息
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ModelMap saveConfig(UasSchedular schedular) {
        UasSchedular newSchedular = schedularService.save(schedular);
        useLogService.appendLog(UseType.SET_UAS_SCHEDULAR.code(), schedular.getId() == null ? "新增" : "修改", AgentUtils.getIp(request));
        return success(newSchedular);
    }

    /**
     * 删除配置信息
     *
     * @param id id
     * @return
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    public ModelMap deleteConfig(@PathVariable("id") Long id) {
        schedularService.deleteById(id);
        useLogService.appendLog(UseType.DEL_UAS_SCHEDULAR.code(), "id: " + id, AgentUtils.getIp(request));
        return success();
    }

    /**
     * 获取分页信息
     *
     * @param params 分页参数
     * @param keyword 关键字
     * @param request 请求
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Page<UasSchedular> findByPageInfo(PageParams params, String keyword, HttpServletRequest request) {
        PageInfo info = new PageInfo(params);
        Sort sort = new Sort(Sort.Direction.ASC, "id");
        info.setSort(sort);
        if (!StringUtils.isEmpty(keyword)) {
            keyword = keyword.trim();
        }
        useLogService.appendLog(UseType.UAS_SCHEDULAR_SEARCH.code(), keyword, AgentUtils.getIp(request));
        return schedularService.findConfigPageByKeyword(info, keyword);
    }
}
