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

import com.uas.platform.b2b.manage.controller.support.PageResultBean;
import com.uas.platform.b2b.manage.controller.support.ResultBean;
import com.uas.platform.b2b.manage.model.DeputyOrder;
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.DeputyOrderService;
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.Sort;
import org.springframework.util.StringUtils;
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;


/**
 * 代采订单
 *
 * @author hejq
 * @date 2018-11-29 9:32
 */
@RequestMapping("/deputyOrder")
@RestController
public class DeputyOrderController extends BaseController {

    @Autowired
    private DeputyOrderService deputyOrderService;

    /**
     * 分页查询代采订单新
     *
     * @param params 分页信息
     * @param keyword 搜索词
     * @return 查询结果
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public PageResultBean<DeputyOrder> findByPageInfo(PageParams params, String keyword) {
        PageInfo info = new PageInfo(params);
        Sort sort = new Sort(Sort.Direction.DESC, "date");
        info.setSort(sort);
        if (!StringUtils.isEmpty(keyword)) {
            keyword = keyword.trim();
        }
        useLogService.appendLog(UseType.DEPUTY_ORDER_SEARCH.code(), keyword, AgentUtils.getIp(request));
        return new PageResultBean<>(deputyOrderService.findByPageInfo(info, keyword));
    }

    /**
     * 重新下载代采订单到ERP
     *
     * @param id 单据id
     * @return 操作结果
     */
    @RequestMapping(value = "/reDownOrder/{id}", method = RequestMethod.POST)
    public ResultBean editDeputyOrder(@PathVariable("id") Long id) {
        useLogService.appendLog(UseType.DEPUTY_ORDER_REDOWNLOAD.code(), "id" + id, AgentUtils.getIp(request));
        return deputyOrderService.reDownOrder(id);
    }
}
