package com.ltgk.port.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ltgk.common.annotation.Log;
import com.ltgk.common.annotation.RepeatSubmit;
import com.ltgk.common.core.controller.BaseController;
import com.ltgk.common.core.domain.AjaxResult;
import com.ltgk.common.core.page.TableDataInfo;
import com.ltgk.common.enums.BusinessType;
import com.ltgk.common.utils.SecurityUtils;
import com.ltgk.common.utils.StringUtils;
import com.ltgk.common.utils.bean.BeanUtils;
import com.ltgk.common.utils.poi.ExcelUtil;
import com.ltgk.common.validation.groups.Add;
import com.ltgk.common.validation.groups.Update;
import com.ltgk.port.domain.dto.DsPortEquDTO;
import com.ltgk.port.domain.entity.DsPort;
import com.ltgk.port.domain.entity.DsPortEqu;
import com.ltgk.port.service.IDsPortEquService;
import com.ltgk.port.service.IDsPortService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.xmlbeans.impl.soap.Detail;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 渔港设备 前端控制器
 *
 * @author Qi ChengBin
 * @date 2024-12-11
 */
@Api(tags = "渔港设备")
@RestController
@RequestMapping("/fishingPort/dsPortEqu")
public class DsPortEquController extends BaseController {

    @Resource
    private IDsPortEquService dsPortEquService;
    @Resource
    private IDsPortService dsPortService;

    /**
     * 分页查询渔港设备列表
     *
     * @param dsPortEquDTO: 渔港设备信息数据传输对象
     * @return com.ltgk.common.core.page.TableDataInfo
     * @author Qi ChengBin
     * @date 2024-12-11
     */
    @PostMapping("/page")
    @ApiOperation(value = "分页查询渔港设备列表")
    public TableDataInfo page(@RequestBody DsPortEquDTO dsPortEquDTO) {
        LambdaQueryWrapper<DsPortEqu> wrapper = getDsPortEquLambdaQueryWrapper(dsPortEquDTO);
        startPage(dsPortEquDTO.getPageNum(), dsPortEquDTO.getPageSize());
        List<DsPortEqu> dsPortEquList = dsPortEquService.list(wrapper);
        return getDataTable(dsPortEquList);
    }

    /**
     * 导出渔港设备列表
     *
     * @param dsPortEquDTO: 渔港设备信息数据传输对象
     * @author Qi ChengBin
     * @date 2024-12-11
     */
    @Log(title = "渔港设备", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出渔港设备数据")
    public void export(HttpServletResponse response, @RequestBody DsPortEquDTO dsPortEquDTO) {
        LambdaQueryWrapper<DsPortEqu> wrapper = getDsPortEquLambdaQueryWrapper(dsPortEquDTO);
        List<DsPortEqu> dsPortEquList = dsPortEquService.list(wrapper);
        ExcelUtil<DsPortEqu> util = new ExcelUtil<DsPortEqu>(DsPortEqu.class);
        util.exportExcel(response, dsPortEquList, "渔港设备数据");
    }

    /**
     * 获取渔港设备详细信息
     *
     * @param dsPortEquDTO: 渔港设备信息数据传输对象
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2024-12-11
     */
    @PostMapping(value = "/detail")
    @ApiOperation(value = "获取渔港设备详细数据")
    public AjaxResult detail(@Validated(Detail.class) @RequestBody DsPortEquDTO dsPortEquDTO) {
        return success(dsPortEquService.getById(dsPortEquDTO.getId()));
    }

    /**
     * 新增渔港设备信息
     *
     * @param dsPortEqu: 设备信息实体对象
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2024-12-11
     */
    @RepeatSubmit(message = "请勿重复提交")
    @Log(title = "新增渔港设备", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation(value = "新增渔港设备数据")
    public AjaxResult add(@Validated(Add.class) @RequestBody DsPortEqu dsPortEqu) {

        DsPort dsPort = dsPortService.getById(dsPortEqu.getPortId());
        if (Objects.isNull(dsPort)) {
            return AjaxResult.error("所选渔港不存在，请先添加渔港信息！");
        }
        String userName = SecurityUtils.getLoginUser().getUser().getNickName();
        dsPortEqu.setAreaCode(dsPort.getAreaCode())
                .setEquAddr(dsPort.getPortName())
                .setCreateBy(userName)
                .setCreateAt(new Date())
                .setUpdateBy(userName)
                .setUpdateAt(new Date());
        return toAjax(dsPortEquService.save(dsPortEqu));
    }

    /**
     * 修改渔港设备信息
     *
     * @param dsPortEqu: 渔港设备信息实体对象
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2024-12-11
     */
    @Log(title = "修改渔港设备", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ApiOperation(value = "修改渔港设备数据")
    public AjaxResult edit(@Validated(Update.class) @RequestBody DsPortEqu dsPortEqu) {
        String userName = SecurityUtils.getLoginUser().getUser().getNickName();
        dsPortEqu.setUpdateBy(userName)
                .setUpdateAt(new Date());
        return toAjax(dsPortEquService.updateById(dsPortEqu));
    }

    /**
     * 删除渔港设备信息
     *
     * @param dsPortEquDTO:
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2024-12-11
     */
    @Log(title = "删除渔港设备", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ApiOperation(value = "批量删除渔港设备数据")
    public AjaxResult delete(@Validated(Detail.class) @RequestBody DsPortEquDTO dsPortEquDTO) {
        return toAjax(dsPortEquService.removeById(dsPortEquDTO.getId()));
    }

    /**
     * 组装港内设备查询 wrapper
     *
     * @param dsPortEquDTO:
     * @return com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<com.ltgk.port.domain.entity.DsPortEqu>
     * @author Qi ChengBin
     * @date 2024/12/17 16:16
     */
    private static LambdaQueryWrapper<DsPortEqu> getDsPortEquLambdaQueryWrapper(DsPortEquDTO dsPortEquDTO) {
        DsPortEqu dsPortEqu = getDsPortEqu(dsPortEquDTO);
        // 开始时间和结束时间 -- 生产日期
        String startTime = dsPortEquDTO.getStartTime();
        String endTime = dsPortEquDTO.getEndTime();

        LambdaQueryWrapper<DsPortEqu> wrapper = new LambdaQueryWrapper<>(dsPortEqu);
        wrapper.ge(StringUtils.isNotBlank(startTime), DsPortEqu::getEquManufactureDate, startTime)
                .le(StringUtils.isNotBlank(endTime), DsPortEqu::getEquManufactureDate, endTime)
                .orderByDesc(DsPortEqu::getUpdateAt);
        return wrapper;
    }

    /**
     * 通过渔港设备数据传输对象获取渔港设备实体
     *
     * @param dsPortEquDTO: 渔港设备数据传输对象
     * @return com.ltgk.port.domain.entity.DsPortEqu
     * @author Qi ChengBin
     * @date 2024/12/26 09:33
     */
    private static DsPortEqu getDsPortEqu(DsPortEquDTO dsPortEquDTO) {
        dsPortEquDTO.queryCheck();
        DsPortEqu dsPortEqu = new DsPortEqu();
        BeanUtils.copyProperties(dsPortEquDTO, dsPortEqu);
        return dsPortEqu;
    }
}
