package com.unis.crk.cplbusiness.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.unis.crk.cplbusiness.entity.LiOrder;
import com.unis.crk.cplbusiness.entity.vo.LiOrderVo;
import com.unis.crk.cplbusiness.service.ILiOrderService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.unis.crk.utils.ResultUtil;
import org.springframework.util.StringUtils;

import io.swagger.annotations.ApiOperation;
import cn.hutool.json.JSONObject;

import java.util.Map;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ghh
 * @since 2023-01-20
 */
@CrossOrigin
@Api(value = "卖家发货订单",tags = {"卖家发货订单"})
@RestController
@RequestMapping("liOrder")
public class LiOrderController {

    private final static Logger LOGGER = LoggerFactory.getLogger(LiOrderController.class);


    @Autowired
    private ILiOrderService liOrderService;


    /**
     * 根据ID查询数据
     * @param id
     * @return
     */
    @ApiOperation(value="单条数据查询查询", notes="根据ID查询数据")
    @GetMapping("getOne/{id}")
    public ResultUtil getOne(@PathVariable("id") String id){
        LOGGER.info("入参为:{}",id);
        ResultUtil<LiOrder> data = new ResultUtil<>();
        if (StringUtils.isEmpty(id)){
            data.failed("传入的主键为空");
            return data;
        }
        data.success(this.liOrderService.getByIdGetOrder(id));
        return data;
    }
    /**
     * 分页高级查询
     * @return
     */
    @ApiOperation(value="分页查询", notes="分页高级查询")
    @PostMapping(value="/findPage")
    public ResultUtil findPage(@RequestBody LiOrderVo pageQuery) {
        ResultUtil<IPage<Map<String,Object>>> data = new ResultUtil<>();
        IPage<Map<String, Object>> pageVo = liOrderService.findPageVo(pageQuery);
        data.success(this.liOrderService.findPageVo(pageQuery));
        return data;
    }


    /**
     * 保存电商订单出库
     * @return
     */
    @ApiOperation(value="保存电商订单", notes="保存电商订单")
    @PostMapping(value="/save")
    public ResultUtil save(@RequestBody LiOrder param) {
            LOGGER.info("入参为:{}", param);
            ResultUtil<Boolean> data = new ResultUtil<>();
            if (ObjectUtils.isEmpty(param)){
                data.failed("所接收对象为null");
                return data;
            }
            JSONObject ret = new JSONObject();
            try {
                ret  = this.liOrderService.saveOrder(param);
            } catch (Exception e) {
                e.printStackTrace();
                ret.set("success", false);
                ret.set("msg", e.getMessage());
            }

            if (ret.getBool("success")){
                data.success(ret.getStr("msg"));
            }else {
                data.failed(ret.getStr("msg"));
            }
            return  data;
    }

    /**
     * 根据ID删除数据
     * @param id
     * @return
     */
    @ApiOperation(value="删除", notes="根据ID删除数据")
    @DeleteMapping("delete/{id}")
    public ResultUtil delete(@PathVariable("id") String id){
        LOGGER.info("入参为:{}", id);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (StringUtils.isEmpty(id)){
            data.failed("所接收id为null");
            return data;
        }
        JSONObject ret = new JSONObject();
        try {
            ret  = this.liOrderService.deleteOrderCk(id);
        } catch (Exception e) {
            e.printStackTrace();
            ret.set("success", false);
            ret.set("msg", e.getMessage());
        }

        if (ret.getBool("success")){
            data.success(ret.getStr("msg"));
        }else {
            data.failed(ret.getStr("msg"));
        }
        return data;
    }




}

