KuaidiHeaderController.java 12.4 KB
package com.huaheng.pc.shipment.kuaidiHeader.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huaheng.api.WAYBILLNO.service.KuaidiService;
import com.huaheng.common.support.Convert;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.Wrappers;
import com.huaheng.common.utils.poi.ExcelUtil;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.aspectj.lang.annotation.Log;
import com.huaheng.framework.aspectj.lang.constant.BusinessType;
import com.huaheng.framework.web.controller.BaseController;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.framework.web.page.PageDomain;
import com.huaheng.framework.web.page.TableDataInfo;
import com.huaheng.framework.web.page.TableSupport;
import com.huaheng.pc.config.KDCertification.domain.KDCertification;
import com.huaheng.pc.config.KDCertification.service.KDCertificationService;
import com.huaheng.pc.shipment.kuaidiHeader.domain.KuaidiHeader;
import com.huaheng.pc.shipment.kuaidiHeader.service.KuaidiHeaderService;
import com.huaheng.pc.system.dict.domain.DictData;
import com.huaheng.pc.system.dict.service.IDictDataService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 【请填写功能名称】 信息操作处理
 *
 * @author huaheng
 * @date 2022-09-07
 */
@Controller
@RequestMapping("/shipment/kuaidiHeader")
public class KuaidiHeaderController extends BaseController {
    private String prefix = "shipment/kuaidiHeader";

    @Resource
    private KuaidiHeaderService kuaidiHeaderService;
    @Resource
    private IDictDataService dictDataService;
    @Resource
    private KDCertificationService kdCertificationService;
    @Resource
    private KuaidiService kuaidiService;

    @RequiresPermissions("shipment:kuaidiHeader:view")
    @GetMapping()
    public String kuaidiHeader() {
        return prefix + "/list";
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("shipment:kuaidiHeader:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(KuaidiHeader kuaidiHeader, String startTime, String endTime) {
        LambdaQueryWrapper<KuaidiHeader> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(KuaidiHeader::getDeleted, false);
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(kuaidiHeader.getSourceCode()), KuaidiHeader::getSourceCode, kuaidiHeader.getSourceCode());
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(kuaidiHeader.getTrackCode()), KuaidiHeader::getTrackCode, kuaidiHeader.getTrackCode());
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(kuaidiHeader.getOrderBill()), KuaidiHeader::getOrderBill, kuaidiHeader.getOrderBill());
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(kuaidiHeader.getCode()), KuaidiHeader::getCode, kuaidiHeader.getCode());
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(kuaidiHeader.getCompanyCode()), KuaidiHeader::getCompanyCode, kuaidiHeader.getCompanyCode());
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(kuaidiHeader.getUWarehouseCode()), KuaidiHeader::getUWarehouseCode, kuaidiHeader.getUWarehouseCode());
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(kuaidiHeader.getCreatedBy()), KuaidiHeader::getCreatedBy, kuaidiHeader.getCreatedBy());
        lambdaQueryWrapper.ge(StringUtils.isNotEmpty(startTime), KuaidiHeader::getCreated, startTime)
                .le(StringUtils.isNotEmpty(endTime), KuaidiHeader::getCreated, endTime);
        lambdaQueryWrapper.orderByDesc(KuaidiHeader::getKuaidiCom, KuaidiHeader::getCreated);
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) {
            /*使用分页查询*/
            Page<KuaidiHeader> page = new Page<>(pageNum, pageSize);
            IPage<KuaidiHeader> iPage = kuaidiHeaderService.page(page, lambdaQueryWrapper);
            return getMpDataTable(iPage.getRecords(), iPage.getTotal());
        } else {
            List<KuaidiHeader> list = kuaidiHeaderService.list(lambdaQueryWrapper);
            return getDataTable(list);
        }
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @RequiresPermissions("shipment:kuaidiHeader:add")
    @Log(title = "【请填写功能名称】", action = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(KuaidiHeader kuaidiHeader) {
        return kuaidiHeaderService.saveHeader(kuaidiHeader);
    }

    /**
     * 修改【请填写功能名称】
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        KuaidiHeader kuaidiHeader = kuaidiHeaderService.getById(id);
        mmap.put("kuaidiHeader", kuaidiHeader);
        return prefix + "/edit";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("shipment:kuaidiHeader:edit")
    @Log(title = "【请填写功能名称】", action = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(KuaidiHeader kuaidiHeader) {
        return toAjax(kuaidiHeaderService.updateById(kuaidiHeader));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("shipment:kuaidiHeader:remove")
    @Log(title = "【请填写功能名称】", action = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        if (StringUtils.isEmpty(ids)) {
            return AjaxResult.error("id不能为空");
        }
        for (Integer id : Convert.toIntArray(ids)) {
            KuaidiHeader kuaidiHeader = kuaidiHeaderService.getById(id);
            if (kuaidiHeader == null) {
                return AjaxResult.error("没有此快递单");
            }
            kuaidiHeader.setDeleted(true);
            boolean tag = kuaidiHeaderService.updateById(kuaidiHeader);
            if (!tag) {
                return AjaxResult.error("删除失败");
            }
        }
        return AjaxResult.success("删除成功");
    }

    /**
     * U8导入快递信息
     */
    @GetMapping("/U8Save")
    public String U8Save() {
        return prefix + "/save";
    }

    @RequiresPermissions("shipment:kuaidiHeader:add")
    @Log(title = "出库-快递单", operating = "新增快递单主单", action = BusinessType.INSERT)
    @PostMapping("/save")
    @ResponseBody
    public AjaxResult save(KuaidiHeader kuaidiHeader) {
        AjaxResult ajaxResult = kuaidiHeaderService.saveU8Kuaidi(kuaidiHeader);
        return ajaxResult;
    }

    @PostMapping("/getKuaidiHeader")
    @ResponseBody
    public AjaxResult<KuaidiHeader> getKuaidiHeader(int id) {
        return AjaxResult.success(kuaidiHeaderService.getById(id));
    }

    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<KuaidiHeader> util = new ExcelUtil<>(KuaidiHeader.class);
        List<KuaidiHeader> list = util.importExcel(file.getInputStream());
        String operName = ShiroUtils.getLoginName();
        Boolean flag = true;
        flag = kuaidiHeaderService.importData(list, false);
        if (flag == true) {
            return AjaxResult.success("导入成功");
        } else {
            return AjaxResult.success("导入失败,数据格式不对");
        }
    }

    /**
     * 导出模板
     *
     * @return
     * @throws Exception
     */
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {
        ExcelUtil<KuaidiHeader> util = new ExcelUtil<>(KuaidiHeader.class);
        return util.importTemplateExcel("快递导入");
    }

    //	@RequiresPermissions("shipment:kuaidi:edit")
    @Log(title = "出库-快递单", operating = "快递导入", action = BusinessType.OTHER)
    @PostMapping("/importKD")
    @ResponseBody
    public AjaxResult importKD(KuaidiHeader kuaidiHeader) {
        AjaxResult ajaxResult = kuaidiHeaderService.sendKD(kuaidiHeader);
        return ajaxResult;
    }

    @Log(title = "出库-快递单", operating = "快递批量导入", action = BusinessType.OTHER)
    @PostMapping("/importKDS")
    @ResponseBody
    public AjaxResult importKDS(KuaidiHeader kuaidiHeader) {
        AjaxResult ajaxResult = kuaidiHeaderService.sendKDS(kuaidiHeader);
        return ajaxResult;
    }

    //	@RequiresPermissions("shipment:kuaidi:edit")
    @Log(title = "出库-快递单", operating = "快递打印", action = BusinessType.OTHER)
    @PostMapping("/printKD")
    @ResponseBody
    public AjaxResult printKD(Integer id) {
        KuaidiHeader kuaidiHeader = new KuaidiHeader();
        kuaidiHeader = kuaidiHeaderService.getById(id);
        if (kuaidiHeader == null) {
            return AjaxResult.error("要打印的单据不存在");
        }
        AjaxResult ajaxResult = kuaidiHeaderService.printKD(kuaidiHeader.getKuaidiNum(), kuaidiHeader.getWarehouseCode());
        return ajaxResult;
    }

    //	@RequiresPermissions("shipment:kuaidi:edit")
    @Log(title = "出库-快递单", operating = "快递批量打印", action = BusinessType.OTHER)
    @PostMapping("/printKDS")
    @ResponseBody
    public AjaxResult printKDS(String ids) {
        AjaxResult ajaxResult = kuaidiHeaderService.printKDS(ids);
        return ajaxResult;
    }

    /**
     * 选择快递公司
     */
    @GetMapping("/importt/{id}")
    public String editt(@PathVariable("id") Integer id, ModelMap mmap) {
        DictData dictData = new DictData();
        dictData.setDictType("kuaidi");
        dictData = dictDataService.selectModel(dictData);
        KuaidiHeader kuaidiHeader = new KuaidiHeader();
        kuaidiHeader.setId(id);
        if (dictData != null) {
            kuaidiHeader.setKuaidiCom(dictData.getDictValue());
        }
        mmap.put("kuaidiHeader", kuaidiHeader);
        return prefix + "/import";
    }

    /**
     * 批量选择快递公司
     */
    @GetMapping("/importKDS/{ids}")
    public String importKDS(@PathVariable("ids") String ids, ModelMap mmap) {
        DictData dictData = new DictData();
        dictData.setDictType("kuaidi");
        dictData = dictDataService.selectModel(dictData);
        KuaidiHeader kuaidiHeader = new KuaidiHeader();
        kuaidiHeader.setOrderBill(ids);
        if (dictData != null) {
            kuaidiHeader.setKuaidiCom(dictData.getDictValue());
        }
        mmap.put("kuaidiHeader", kuaidiHeader);
        return prefix + "/imports";
    }

    @RequiresPermissions("shipment:kuaidiHeader:fresh")
    @Log(title = "出库-快递单", operating = "刷新token", action = BusinessType.OTHER)
    @PostMapping("/fresh")
    @ResponseBody
    public AjaxResult freshKdToken() throws Exception {
        KDCertification kdCertification = new KDCertification();
//		kdCertification.setId("1");
        kdCertification.setWarehouseCode(ShiroUtils.getWarehouseCode());
        LambdaQueryWrapper<KDCertification> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(KDCertification::getWarehouseCode, ShiroUtils.getWarehouseCode());
        kdCertification = kdCertificationService.getOne(wrapper);
        if (kdCertification == null) {
            return AjaxResult.error("没有此token");
        }
        String json = kuaidiService.refreshTokenByClass(kdCertification);
        Map hashMap = JSON.parseObject(json, HashMap.class);
        if (!hashMap.isEmpty() && StringUtils.isNotNull(hashMap.get("access_token")) && StringUtils.isNotNull(hashMap.get("refresh_token"))) {
            kdCertification.setAccessToken(hashMap.get("access_token").toString());
            kdCertification.setRefreshToken(hashMap.get("refresh_token").toString());
            kdCertificationService.updateById(kdCertification);
        } else {
            return AjaxResult.error(hashMap.get("message").toString());
        }
        return AjaxResult.success("成功");
    }


}