package com.yunfeiyun.agmp.fms.controller;

import com.yunfeiyun.agmp.common.annotation.Log;
import com.yunfeiyun.agmp.common.core.controller.BaseController;
import com.yunfeiyun.agmp.common.core.domain.AjaxResult;
import com.yunfeiyun.agmp.common.core.page.TableDataInfo;
import com.yunfeiyun.agmp.common.enums.BusinessType;
import com.yunfeiyun.agmp.common.enums.SpmOrderStatus;
import com.yunfeiyun.agmp.common.utils.poi.ExcelUtil;
import com.yunfeiyun.agmp.fms.domain.SpmOrder;
import com.yunfeiyun.agmp.fms.domain.reqvo.SpmOrderLogisticsInfoReqVo;
import com.yunfeiyun.agmp.fms.domain.reqvo.SpmOrderMatchTraceCodeReqVo;
import com.yunfeiyun.agmp.fms.domain.resvo.SpmOrderLogisticsInfoResVo;
import com.yunfeiyun.agmp.fms.domain.resvo.SpmOrderLogisticsSumInfoResVo;
import com.yunfeiyun.agmp.fms.domain.resvo.SpmOrderResVo;
import com.yunfeiyun.agmp.fms.service.ISpmOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 农产品出库登记单Controller
 * 
 * @author 杨晓辉
 * @date 2023-07-06
 */
@RestController
@RequestMapping("/spm/order")
public class SpmOrderController extends BaseController
{
    @Autowired
    private ISpmOrderService spmOrderService;

    /**
     * 查询农产品出库登记单列表
     */
    @PreAuthorize("@ss.hasPermi('spm:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(SpmOrder spmOrder)
    {
        startPage();
        List<SpmOrderResVo> list = spmOrderService.selectSpmOrderResVoList(spmOrder);
        return getDataTable(list);
    }
    /**
     * 查询农产品物流列表
     */
    @PreAuthorize("@ss.hasPermi('spm:order:logistics:list')")
    @GetMapping("/logistics/list")
    public TableDataInfo logisticsList(SpmOrderLogisticsInfoReqVo spmOrderLogisticsInfoReqVo)
    {
        startPage();
        List<SpmOrderLogisticsInfoResVo> list = spmOrderService.selectSpmOrderLogisticsInfoReqVo(spmOrderLogisticsInfoReqVo);
        return getDataTable(list);
    }
    /**
     * 查询农产品物流汇总信息
     */
    @PreAuthorize("@ss.hasPermi('spm:order:logistics:sum')")
    @GetMapping("/logistics/sum")
    public AjaxResult logisticsSum(SpmOrderLogisticsInfoReqVo spmOrderLogisticsInfoReqVo)
    {

        List<SpmOrderLogisticsSumInfoResVo> selectSpmOrderLogisticsSumInfo  = spmOrderService.selectSpmOrderLogisticsSumInfo(spmOrderLogisticsInfoReqVo);
        return AjaxResult.success(selectSpmOrderLogisticsSumInfo);
    }

    /**
     * 导出农产品出库登记单列表
     */
    @PreAuthorize("@ss.hasPermi('spm:order:export')")
    @Log(title = "农产品出库登记单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SpmOrder spmOrder)
    {
        List<SpmOrderResVo> list = spmOrderService.selectSpmOrderResVoList(spmOrder);
        ExcelUtil<SpmOrderResVo> util = new ExcelUtil<>(SpmOrderResVo.class);
        util.exportExcel(response, list, "农产品出库登记单数据");
    }

    /**
     * 获取农产品出库登记单详细信息
     */
    @PreAuthorize("@ss.hasPermi('spm:order:info')")
    @GetMapping(value = "/info/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") String orderId)
    {
        return success(spmOrderService.selectSpmOrderByOrderId(orderId));
    }

    /**
     * 销售下单
     */
    @PreAuthorize("@ss.hasPermi('spm:order:sale')")
    @PostMapping(value = "/sale")
    public AjaxResult sale(@RequestBody SpmOrder spmOrder){
        spmOrderService.sale(spmOrder);
        return success();
    }

    /**
     * 发货
     */
    @PreAuthorize("@ss.hasPermi('spm:order:deliver')")
    @PostMapping(value = "/deliver")
    public AjaxResult deliver(@RequestBody SpmOrder spmOrder){
        spmOrderService.deliver(spmOrder);
        return success();
    }
    /**
     * 取消发货
     */
    @PreAuthorize("@ss.hasPermi('spm:order:cancel')")
    @PostMapping(value = "/cancel")
    public AjaxResult cancel(@RequestBody SpmOrder spmOrder){
        if(spmOrderService.updateSpmOrder(spmOrder)==1){
            return success();
        }else {
            return error();
        }
    }
    /**
     * 送达
     */
    @PreAuthorize("@ss.hasPermi('spm:order:delivered:batch')")
    @PostMapping(value = "/delivered/batch")
    public AjaxResult delivered(@RequestBody String[] ids){
        SpmOrder spmOrder = new SpmOrder();
        spmOrder.setOrderStatus(SpmOrderStatus.received.getCode());
        if(spmOrderService.updateSpmOrderByIds(spmOrder,ids)==1){
            return success();
        }else {
            return error();
        }
    }

    /**
     * 订单关联溯源码
     */
    @PreAuthorize("@ss.hasPermi('spm:order:tracecode:match')")
    @PostMapping(value = "/tracecode/match")
    public AjaxResult tracecodeMatch(@RequestBody SpmOrderMatchTraceCodeReqVo spmOrderMatchTraceCodeReqVo){
        if(spmOrderService.traceCodeMatch(spmOrderMatchTraceCodeReqVo)){
            return success();
        }else {
            return error();
        }
    }
    /**
     * 溯源码解绑
     */
    @PreAuthorize("@ss.hasPermi('spm:order:tracecode:unbind')")
    @PostMapping(value = "/tracecode/unbind")
    public AjaxResult traceCodeUnbind(@RequestParam String tarcecodeapplyno){
        if(spmOrderService.traceCodeUnbind(tarcecodeapplyno)){
            return success();
        }else {
            return error();
        }
    }




}
