package com.ruoyi.web.controller.app;

import java.util.*;

import javax.servlet.http.HttpServletResponse;

import com.ruoyi.app.DTO.HookBindBatchListDTO;
import com.ruoyi.app.model.request.AddHookBindBatch;
import com.ruoyi.app.model.request.EditHookBindBatch;
import com.ruoyi.app.model.request.ReqHookBindDetail;
import com.ruoyi.app.model.request.ReqHookBindList;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
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.validation.annotation.Validated;
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.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.app.model.HookBind;
import com.ruoyi.app.service.IHookBindService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import static com.ruoyi.common.utils.uuid.IdUtils.fastSimpleUUID;

/**
 * 吊钩绑定Controller
 * 
 * @author coede
 * @date 2025-03-19
 */
@Api("吊钩绑定信息管理")
@RestController
@RequestMapping("/app/hookBind")
public class HookBindController extends BaseController
{
    @Autowired
    private IHookBindService hookBindService;

    /**
     * 查询吊钩绑定列表
     */
    @ApiOperation("查询吊钩绑定列表")
    @GetMapping("/list")
    public TableDataInfo list(HookBind hookBind)
    {
        startPage();
        List<HookBind> list = hookBindService.selectHookBindList(hookBind);
        return getDataTable(list);
    }

    /**
     * 按批次查询吊钩绑定列表
     */
    @ApiOperation("按批次查询吊钩绑定列表")
    @GetMapping("/batchList")
    public TableDataInfo batchList(ReqHookBindList req)
    {
        startPage();
        List<HookBindBatchListDTO> list = hookBindService.selectHookBindListByBatch(req);
        return getDataTable(list);
    }

    /**
     * 导出吊钩绑定列表
     */
    @ApiOperation("导出吊钩绑定列表")
    // @PreAuthorize("@ss.hasPermi('app:hookBind:export')")
    @Log(title = "吊钩绑定", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HookBind hookBind)
    {
        List<HookBind> list = hookBindService.selectHookBindList(hookBind);
        ExcelUtil<HookBind> util = new ExcelUtil<HookBind>(HookBind.class);
        util.exportExcel(response, list, "吊钩绑定数据");
    }

    /**
     * 获取吊钩绑定详细信息
     */
    @ApiOperation("获取吊钩绑定详细信息")
    @GetMapping(value = "/group")
    public AjaxResult getGroupInfo(@Validated ReqHookBindDetail req)
    {
        return success(hookBindService.selectHookBindByBatch(req.getBatchNo()));
    }

    /**
     * 获取吊钩绑定详细信息
     */
    @ApiOperation("获取吊钩绑定详细信息")
    @GetMapping(value = "/detail/{hookNo}")
    public AjaxResult getDetail(@PathVariable("hookNo") String hookNo)
    {
        return success(hookBindService.selectHookBindOnlyDetail(hookNo));
    }

    /**
     * 新增吊钩绑定 暂时不用
     */
    @ApiOperation("新增吊钩绑定 暂时不用")
    // @PreAuthorize("@ss.hasPermi('app:hookBind:add')")
    @Log(title = "吊钩绑定", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated  @RequestBody HookBind hookBind)
    {
        hookBind.setCreateBy(getUsername());
        return toAjax(hookBindService.insertHookBind(hookBind));
    }

    /**
     * 新增吊钩批量绑定
     */
    @ApiOperation("新增吊钩批量绑定")
    // @PreAuthorize("@ss.hasPermi('app:hookBind:add')")
    @Log(title = "吊钩绑定", businessType = BusinessType.INSERT)
    @PostMapping("/addBatch")
    public AjaxResult addBatch(@Validated  @RequestBody AddHookBindBatch req)
    {
        try {
            //生成新对象
            List<HookBind> hookBindArr = new ArrayList<>();
            String batchNo = fastSimpleUUID();
            //先判断本身数组内是否有重复的
            Set<String> uniqueIds = new HashSet<>();

            for (String hook : req.getHookNoListArr()) {
                //noinspection ConstantConditions  忽略错误的IDEA提示
                if (!uniqueIds.add(hook)) {
                    return error("新增吊钩批量绑定，本次添加存在相同的吊钩号");
                }
                HookBind hookBind = new HookBind();
                hookBind.setBatchNo(batchNo);
                hookBind.setHookNo(hook);
                hookBind.setBindTime(req.getBindTime());
                hookBind.setDistributeBatchId(req.getDistributeBatchId());
                hookBind.setSlaughterCode(req.getSlaughterCode());
                hookBind.setCreateTime(DateUtils.getNowDate());
                hookBind.setCreateBy(getUsername());

                //未传绑定时间默认当前时间
                if (req.getBindTime() == null) {
                    hookBind.setBindTime(DateUtils.getNowDate());
                }else{
                    hookBind.setBindTime(req.getBindTime());
                }
                hookBindArr.add(hookBind);
            }
            //校验数据库的
            for (HookBind hookBind : hookBindArr) {

                HookBind check = hookBindService.checkHookNoUnique(hookBind);
                if (StringUtils.isNotNull(check)) {
                    return error("新增吊钩批量绑定失败，'" + check.getHookNo() + "'同一时间已存在绑定");
                }
            }
            return toAjax(hookBindService.insertHookBindBatch(hookBindArr,req.getHookNoListArr()));
        }catch(Exception e) {
                return error("新增吊钩批量绑定失败:"+e.getMessage());
        }
    }

    /**
     * 修改吊钩绑定
     */
    @ApiOperation("修改吊钩绑定")
    // @PreAuthorize("@ss.hasPermi('app:hookBind:edit')")
    @Log(title = "吊钩绑定", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody HookBind hookBind)
    {

        hookBind.setUpdateBy(getUsername());
        return toAjax(hookBindService.updateHookBind(hookBind));
    }

    /**
     * 批量修改吊钩绑定
     */
    @ApiOperation("批量修改吊钩绑定")
    // @PreAuthorize("@ss.hasPermi('app:hookBind:edit')")
    @Log(title = "吊钩绑定", businessType = BusinessType.UPDATE)
    @PostMapping("/editBatch")
    public AjaxResult editBatch(@Validated @RequestBody EditHookBindBatch req)
    {
        req.setUpdateBy(getUsername());
        return toAjax(hookBindService.updateHookBindBatch(req));
    }

    /**
     * 删除吊钩绑定
     */
    @ApiOperation("删除吊钩绑定")
    // @PreAuthorize("@ss.hasPermi('app:hookBind:remove')")
    @Log(title = "吊钩绑定", businessType = BusinessType.DELETE)
	@DeleteMapping("/{batchNos}")
    public AjaxResult remove(@PathVariable String[] batchNos)
    {
        return toAjax(hookBindService.deleteHookBindByIds(batchNos));
    }

    /**
     * 清除吊钩绑定关系-不删除数据只是将绑定关系设置为未绑定
     */
    @ApiOperation("清除吊钩绑定关系")
    // @PreAuthorize("@ss.hasPermi('app:hookBind:clean')")
    @Log(title = "吊钩绑定", businessType = BusinessType.UPDATE)
    @PostMapping("/cleanBind")
    public AjaxResult cleanBind()
    {
        return AjaxResult.success(hookBindService.cleanHookBind());
    }
}
