package com.bs.jxc.controller;

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

import com.bs.common.core.domain.model.LoginUser;
import com.bs.common.utils.ServletUtils;
import com.bs.jxc.domain.BmVesselVoyage;
import com.bs.jxc.domain.CmVessel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.bs.common.annotation.Log;
import com.bs.common.core.controller.BaseController;
import com.bs.common.core.domain.AjaxResult;
import com.bs.common.core.page.TableDataInfo;
import com.bs.common.enums.BusinessType;
import com.bs.common.utils.poi.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import cn.hutool.core.lang.Validator;
import com.bs.jxc.domain.CmBerth;
import com.bs.jxc.service.ICmBerthService;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 泊位信息Controller
 *
 * @author bs
 * @date 2023-07-09
 */
@Api(tags = "泊位信息")
@RestController
@RequestMapping("/jxc/berth")
public class CmBerthController extends BaseController {
    @Resource
    private ICmBerthService cmBerthService;

    /**
     * 分页查询泊位信息列表
     */
    @ApiOperation("分页查询泊位信息列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(CmBerth cmBerth) {
        startPage();
        LambdaQueryWrapper<CmBerth> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,cmBerth);
        List<CmBerth> list = cmBerthService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询泊位信息列表
     */
    @ApiOperation("查询泊位信息列表")
    @GetMapping("/list")
    public AjaxResult list(CmBerth cmBerth) {
        LambdaQueryWrapper<CmBerth> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,cmBerth);
        List<CmBerth> list = cmBerthService.list(queryWrapper);
        return success(list);
    }

    /**
     * 导出泊位信息列表
     */
    @ApiOperation("导出泊位信息列表")
    @Log(title = "泊位信息导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmBerth cmBerth) {
        LambdaQueryWrapper<CmBerth> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,cmBerth);
        List<CmBerth> list = cmBerthService.list(queryWrapper);
        ExcelUtil<CmBerth> util = new ExcelUtil<CmBerth>(CmBerth. class);
        util.exportExcel(response, list, "泊位信息数据");
    }

    @Log(title = "泊位信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) {
        try {
            ExcelUtil<CmBerth> util = new ExcelUtil<>(CmBerth.class);
            List<CmBerth> berthList = util.importExcel(file.getInputStream());

            // 创建 LambdaQueryWrapper 对象
            LambdaQueryWrapper<CmBerth> queryWrapper = new LambdaQueryWrapper<>();

            // 根据具体需求进行导入操作
            for (CmBerth berth : berthList) {
                // 按照业务需求构建查询条件
                queryWrapper.eq(CmBerth::getId, berth.getId()); // 示例：根据 ID 判断是否存在

                // 查询数据库中是否已存在相同记录
                Long count = cmBerthService.count(queryWrapper);

                if (count > 0) {
                    // 如果已存在记录，则进行更新操作
                    cmBerthService.update(berth, queryWrapper);
                } else {
                    // 如果不存在记录，则进行插入操作
                    cmBerthService.save(berth);
                }
            }

            return AjaxResult.success("导入成功");
        } catch (Exception e) {
            return AjaxResult.error("导入失败：" + e.getMessage());
        }
    }

    @ApiOperation("导入泊位信息列表模板")
    @Log(title = "导入泊位信息模板", businessType = BusinessType.EXPORT)
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response, CmBerth cmBerth) {
        // 创建一个空的泊位信息列表
        List<CmBerth> emptyList = new ArrayList<>();

        // 导出空列表为 Excel 文件
        ExcelUtil<CmBerth> util = new ExcelUtil<CmBerth>(CmBerth.class);
        util.exportExcel(response, emptyList, "泊位信息模板");
    }


    /**
     * 获取泊位信息详细信息
     */
    @ApiOperation("获取泊位信息详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(cmBerthService.getById(id));
    }

    /**
     * 新增泊位信息
     */
    @ApiOperation("新增泊位信息")
    @Log(title = "泊位信息新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmBerth cmBerth) {
        CmBerth cmBerth1 = cmBerthService.getOne(new LambdaQueryWrapper<CmBerth>().eq(CmBerth::getBerthCode,cmBerth.getBerthCode()));
        if(cmBerth1!=null){
            return error("泊位代码已经存在，请重新输入");
        }


        return toAjax(cmBerthService.save(cmBerth));
    }

    /**
     * 修改泊位信息
     */
    @ApiOperation("修改泊位信息")
    @Log(title = "泊位信息修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmBerth cmBerth) {
        CmBerth cmBerth1= cmBerthService.getOne(new LambdaQueryWrapper<CmBerth>().eq(CmBerth::getBerthCode,cmBerth.getBerthCode()).ne(CmBerth::getId,cmBerth.getId()));
        if(cmBerth1!=null){
            return error("泊位代码已经存在，请重新输入");
        }


        return toAjax(cmBerthService.updateById(cmBerth));
    }

    /**
     * 删除泊位信息
     */
    @ApiOperation("删除泊位信息")
    @Log(title = "泊位信息删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(cmBerthService.removeBatchByIds(ids));
    }

    /**
      * 条件设置
      */
    private void condition (LambdaQueryWrapper<CmBerth> queryWrapper,CmBerth cmBerth){




        //泊位代码
        if(Validator.isNotEmpty(cmBerth.getBerthCode())){
            queryWrapper.like(CmBerth::getBerthCode,cmBerth.getBerthCode());
        }

        //泊位名称
        if(Validator.isNotEmpty(cmBerth.getBerthName())){
            queryWrapper.like(CmBerth::getBerthName,cmBerth.getBerthName());
        }

        //所属港口
        if(Validator.isNotEmpty(cmBerth.getPort())){
            queryWrapper.like(CmBerth::getPort,cmBerth.getPort());
        }

        if(Validator.isNotEmpty(cmBerth.getPortName())){
            queryWrapper.like(CmBerth::getPortName,cmBerth.getPortName());
        }

        //所属公司
        if(Validator.isNotEmpty(cmBerth.getCompanyId())){
            queryWrapper.like(CmBerth::getCompanyId,cmBerth.getCompanyId());
        }




        }

}
