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

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.NoticeRecordService;
import com.uas.platform.b2b.manage.model.NoticeRecordInfo;
import com.uas.platform.b2b.manage.service.UseLogService;
import com.uas.platform.b2b.manage.utils.SearchUtils;
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.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;

/**
 * ERP提醒校验记录
 *
 * @author hejq
 * @date 2018-08-24 14:26
 */
@RestController
@RequestMapping("/noticeRecord")
public class SaleNoticeController extends BaseController {

    @Autowired
    private NoticeRecordService recordService;

    /**
     * 通过分页信息获取发货提醒操作记录等
     *
     * @param params 分页参数
     * @param keyword 关键词
     * @param method 筛选条件
     * @return Page<NoticeRecordInfo>
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Page<NoticeRecordInfo> findByPageInfo(PageParams params, String keyword,
                                                 @RequestParam(value = "method", defaultValue = SearchUtils.SEARCH_ALL, required = false) String method) throws ParseException {
        PageInfo pageInfo = new PageInfo(params);
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        pageInfo.setSort(sort);
        if (!StringUtils.isEmpty(keyword)) {
            keyword = keyword.trim();
        }
        useLogService.appendLog(UseType.SALE_NOTICE_SEARCH.code(), keyword, AgentUtils.getIp(request));
        return recordService.findRecordByPageInfo(pageInfo, keyword, method);
    }

    /**
     * 通过分页信息获取发货提醒操作记录等
     *
     * @return Page<NoticeRecordInfo>
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public ModelMap findById(@PathVariable("id") Long id) {
        useLogService.appendLog(UseType.SALE_NOTICE_DETAIL.code(), String.valueOf(id), AgentUtils.getIp(request));
        return success(recordService.findById(id));
    }
}
