package com.hzs.ground.postage.controller.manage;

import cn.hutool.core.bean.BeanUtil;
import com.hzs.common.core.constant.HttpStatus;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.utils.poi.ExcelUtil;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.scm.postage.ThSupplierPostage;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.ground.postage.param.SupplierPostageParam;
import com.hzs.ground.postage.service.IThSupplierPostageService;
import com.hzs.ground.postage.vo.SupplierPostageVo;
import com.hzs.system.base.ISystemConfigServiceApi;
import com.hzs.system.base.dto.BdCubasdocDTO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/manage/supplier-postage")
public class SupplierPostageController  extends BaseController {
    @Autowired
    private IThSupplierPostageService supplierPostageService;
    @DubboReference
    private ISystemConfigServiceApi systemConfigServiceApi;

    /**
     * 查询列表
     * @return
     */
    @GetMapping("list")
    public TableDataInfo list(SupplierPostageParam supplierPostageParam){
        startPage();

        //   下面代码需要使用 坐等随工提交此方法 取消注释
        R<BdCubasdocDTO> bdCubasdocDate = systemConfigServiceApi.queryBdCubasdocByUser(SecurityUtils.getUserId());
        Integer pkSupplier = null;
        if(bdCubasdocDate.getCode() == HttpStatus.SUCCESS){
            BdCubasdocDTO bdCubasdoc = bdCubasdocDate.getData();
            if(bdCubasdoc == null){
                pkSupplier = 1;
            }else {
                pkSupplier =  bdCubasdoc.getPkSupplier();
            }
        }
        supplierPostageParam.setPkCubasdoc(pkSupplier);
        List<SupplierPostageVo> list = supplierPostageService.selectByQueryList(supplierPostageParam);
        return getDataTable(list);
    }
    /**
     * 查询列表
     * @return
     */
    @PostMapping("export")
    public void export(SupplierPostageParam supplierPostageParam,HttpServletResponse response){

        //   下面代码需要使用 坐等随工提交此方法 取消注释
        R<BdCubasdocDTO> bdCubasdocDate = systemConfigServiceApi.queryBdCubasdocByUser(SecurityUtils.getUserId());
        Integer pkSupplier = null;
        if(bdCubasdocDate.getCode() == HttpStatus.SUCCESS){
            BdCubasdocDTO bdCubasdoc = bdCubasdocDate.getData();
            if(bdCubasdoc == null){
                pkSupplier = 1;
            }else {
                pkSupplier =  bdCubasdoc.getPkSupplier();
            }
        }
        supplierPostageParam.setPkCubasdoc(pkSupplier);
        List<SupplierPostageVo> list = supplierPostageService.selectByQueryList(supplierPostageParam);
        ExcelUtil<SupplierPostageVo> util = new ExcelUtil<>(SupplierPostageVo.class);
        util.exportExcel(response, list, "商品信息导出");
    }

    /**
     * 新增
     * @return
     */
    @PostMapping("save")
    public AjaxResult save(@RequestBody ThSupplierPostage supplierPostage){
        supplierPostage.setCreationTime(DateUtils.currentDateTime());
        supplierPostage.setPkCreator(SecurityUtils.getUserId());
        supplierPostage.setPkCountry(SecurityUtils.getPkCountry());
        supplierPostageService.save(supplierPostage);
        return AjaxResult.success();
    }


    /**
     * 修改
     */
    @PostMapping("update")
    public AjaxResult update(@RequestBody ThSupplierPostage supplierPostage){
        supplierPostageService.updateById(supplierPostage);
        return AjaxResult.success();
    }

    /**
     * 查询详情
     */
    @GetMapping("detail")
    public AjaxResult detail(Integer pkSupplierPostage){

        SupplierPostageVo supplierPostageVo = supplierPostageService.selectByPkId(pkSupplierPostage);

        return AjaxResult.success(supplierPostageVo);
    }

    /**
     * 删除
     * @param pkSupplierPostage
     * @return
     */
    @GetMapping("remove")
    public AjaxResult remove(Integer pkSupplierPostage){
        supplierPostageService.removeById(pkSupplierPostage);
        return AjaxResult.success();
    }
}
