package com.fs.his.controller;

import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.company.service.ICompanyService;
import com.fs.his.domain.FsExpress;
import com.fs.his.service.IFsExpressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 快递公司Controller
 *
 * @author fs
 * @date 2022-03-15
 */
@RestController
@RequestMapping("/his/express")
public class FsExpressController extends BaseController
{
    @Autowired
    private IFsExpressService fsExpressService;

    @Autowired
    private ICompanyService companyService;

    /**
     * 查询快递公司列表
     */
    @PreAuthorize("@ss.hasPermi('his:express:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsExpress fsExpress)
    {
        startPage();
        fsExpress.setIsDel(0);
        List<FsExpress> list = fsExpressService.selectFsExpressList(fsExpress);
        return getDataTable(list);
    }

    @GetMapping("/getExpressList")
    public R getExpressList()
    {
        FsExpress map=new FsExpress();
        map.setIsDel(0);
        List<FsExpress> list = fsExpressService.selectFsExpressList(map);
        return R.ok().put("data",list);
    }

    /**
     * 导出快递公司列表
     */
    @PreAuthorize("@ss.hasPermi('his:express:export')")
    @Log(title = "快递公司", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsExpress fsExpress)
    {
        List<FsExpress> list = fsExpressService.selectFsExpressList(fsExpress);
        ExcelUtil<FsExpress> util = new ExcelUtil<FsExpress>(FsExpress.class);
        return util.exportExcel(list, "express");
    }

    /**
     * 获取快递公司详细信息
     */
    @PreAuthorize("@ss.hasPermi('his:express:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(fsExpressService.selectFsExpressById(id));
    }

    /**
     * 新增快递公司
     */
    @PreAuthorize("@ss.hasPermi('his:express:add')")
    @Log(title = "快递公司", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsExpress fsExpress)
    {
        return toAjax(fsExpressService.insertFsExpress(fsExpress));
    }

    /**
     * 修改快递公司
     */
    @PreAuthorize("@ss.hasPermi('his:express:edit')")
    @Log(title = "快递公司", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsExpress fsExpress)
    {
        return toAjax(fsExpressService.updateFsExpress(fsExpress));
    }

    /**
     * 删除快递公司
     */
    @PreAuthorize("@ss.hasPermi('his:express:remove')")
    @Log(title = "快递公司", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(fsExpressService.deleteFsExpressByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('his:express:allot')")
    @PostMapping("/allotExpress/{omsCode}")
    public R allotExpress(@RequestBody List<Long> companyIds,@PathVariable String omsCode){
        companyService.updateOmsCodeByCompanyIds(companyIds,omsCode);
        return R.ok();
    }

    @GetMapping( "/omsCode/{omsCode}")
    public R getCompanyByOmsCode(@PathVariable("omsCode") String omsCode)
    {
        List<Long> list = companyService.selectCompanyByOmsCode(omsCode);
        return R.ok().put("data2",list);
    }
}
