package com.sundot.airport.web.controller.item;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.sundot.airport.common.core.domain.entity.SysDept;
import com.sundot.airport.common.enums.DeptTypeEnum;
import com.sundot.airport.common.enums.PostTypeEnum;
import com.sundot.airport.item.domain.*;
import com.sundot.airport.system.domain.SysPost;
import com.sundot.airport.system.service.ISysDeptService;
import com.sundot.airport.system.service.ISysPostService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
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.sundot.airport.common.annotation.Log;
import com.sundot.airport.common.core.controller.BaseController;
import com.sundot.airport.common.core.domain.AjaxResult;
import com.sundot.airport.common.enums.BusinessType;
import com.sundot.airport.item.service.IItemSeizureItemsService;
import com.sundot.airport.common.utils.poi.ExcelUtil;
import com.sundot.airport.common.core.page.TableDataInfo;

/**
 * 查获物品明细Controller
 *
 * @author ruoyi
 * @date 2025-07-10
 */
@RestController
@RequestMapping("/item/items")
public class ItemSeizureItemsController extends BaseController {
    @Autowired
    private ISysPostService sysPostService;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private IItemSeizureItemsService itemSeizureItemsService;

    /**
     * 查询查获物品明细列表
     */
    @PreAuthorize("@ss.hasPermi('item:items:list')")
    @GetMapping("/list")
    public TableDataInfo list(ItemSeizureItems itemSeizureItems) {
        startPage();
        List<ItemSeizureItems> list = itemSeizureItemsService.selectItemSeizureItemsList(itemSeizureItems);
        return getDataTable(list);
    }

    /**
     * 导出查获物品明细列表
     */
    @PreAuthorize("@ss.hasPermi('item:items:export')")
    @Log(title = "查获物品明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ItemSeizureItems itemSeizureItems) {
        List<ItemSeizureItems> list = itemSeizureItemsService.selectItemSeizureItemsList(itemSeizureItems);
        ExcelUtil<ItemSeizureItems> util = new ExcelUtil<ItemSeizureItems>(ItemSeizureItems.class);
        util.exportExcel(response, list, "查获物品明细数据");
    }

    /**
     * 获取查获物品明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('item:items:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(itemSeizureItemsService.selectItemSeizureItemsById(id));
    }

    /**
     * 新增查获物品明细
     */
    @PreAuthorize("@ss.hasPermi('item:items:add')")
    @Log(title = "查获物品明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ItemSeizureItems itemSeizureItems) {
        itemSeizureItems.setCreateBy(getUsername());
        return toAjax(itemSeizureItemsService.insertItemSeizureItems(itemSeizureItems));
    }

    /**
     * 修改查获物品明细
     */
    @PreAuthorize("@ss.hasPermi('item:items:edit')")
    @Log(title = "查获物品明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ItemSeizureItems itemSeizureItems) {
        itemSeizureItems.setUpdateBy(getUsername());
        return toAjax(itemSeizureItemsService.updateItemSeizureItems(itemSeizureItems));
    }

    /**
     * 删除查获物品明细
     */
    @PreAuthorize("@ss.hasPermi('item:items:remove')")
    @Log(title = "查获物品明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(itemSeizureItemsService.deleteItemSeizureItemsByIds(ids));
    }

    /**
     * 根据记录ID查询查获物品明细列表
     */
    @PreAuthorize("@ss.hasPermi('item:items:list')")
    @GetMapping("/listByRecorId")
    public AjaxResult listByRecorId(Long recordId) {
        return success(itemSeizureItemsService.listByRecorId(recordId));
    }

    /**
     * 根据筛选条件查询查获物品明细列表
     */
    @PreAuthorize("@ss.hasPermi('item:items:list')")
    @GetMapping("/selectByConditions")
    public TableDataInfo selectByConditions(ItemSeizureParam itemSeizureParam) {
        if (itemSeizureParam.getRecordType() == 1) {
            itemSeizureParam.setInspectUserId(getUserId());
        } else {
            itemSeizureParam.setInspectUserId(null);
            List<Long> userPostList = sysPostService.selectPostListByUserId(getUserId());
            SysDept sysDept = sysDeptService.selectDeptById(getDeptId());
            if (CollectionUtils.isEmpty(userPostList) || ObjectUtils.isEmpty(sysDept)) {
                itemSeizureParam.setInspectUserId(getUserId());
            } else {
                if (sysDept.getDeptType().equals(DeptTypeEnum.STATION.getCode())) {
                    itemSeizureParam.setInspectStationId(sysDept.getDeptId());
                } else if (sysDept.getDeptType().equals(DeptTypeEnum.BRIGADE.getCode())) {
                    itemSeizureParam.setInspectBrigadeId(sysDept.getDeptId());
                } else if (sysDept.getDeptType().equals(DeptTypeEnum.MANAGER.getCode())) {
                    itemSeizureParam.setInspectDepartmentId(sysDept.getDeptId());
                } else if (sysDept.getDeptType().equals(DeptTypeEnum.TEAMS.getCode())) {
                    itemSeizureParam.setInspectTeamId(sysDept.getDeptId());
                } else {
                    itemSeizureParam.setInspectUserId(getUserId());
                }
//                List<SysPost> sysPostList = sysPostService.selectPostAll();
//                sysPostList = sysPostList.stream().filter(sysPost -> userPostList.contains(sysPost.getPostId())).collect(Collectors.toList());
//                PostTypeEnum typeEnum = sysPostList.stream()
//                        .map(item -> {
//                            String postCode = item.getPostCode();
//                            return postCode != null ? PostTypeEnum.getByCode(postCode) : null;
//                        })
//                        .filter(java.util.Objects::nonNull)
//                        .min(Comparator.comparingInt(PostTypeEnum::getLevel))
//                        .orElse(null);
//                if (typeEnum == null || typeEnum == PostTypeEnum.security_inspector) {
//                    itemSeizureParam.setInspectUserId(getUserId());
//                } else {
//                    if (sysDept.getDeptType().equals(DeptTypeEnum.STATION.getCode())) {
//                        itemSeizureParam.setInspectStationId(sysDept.getDeptId());
//                    } else if (sysDept.getDeptType().equals(DeptTypeEnum.DEPARTMENT.getCode())) {
//                        itemSeizureParam.setInspectDepartmentId(sysDept.getDeptId());
//                    } else if (sysDept.getDeptType().equals(DeptTypeEnum.TEAMS.getCode())) {
//                        itemSeizureParam.setInspectTeamId(sysDept.getDeptId());
//                    } else {
//                        itemSeizureParam.setInspectUserId(getUserId());
//                    }
//                }
            }
        }
        startPage();
        List<ItemSeizureDto> list = itemSeizureItemsService.selectByConditions(itemSeizureParam);
        return getDataTable(list);
    }

    /**
     * 根据筛选条件查询分类数量
     */
    @PreAuthorize("@ss.hasPermi('item:items:list')")
    @GetMapping("/selectGroupCount")
    public AjaxResult selectGroupCount(ItemSeizureParam itemSeizureParam) {
        if (itemSeizureParam.getRecordType() == 1) {
            itemSeizureParam.setInspectUserId(getUserId());
        } else {
            itemSeizureParam.setInspectUserId(null);
            List<Long> userPostList = sysPostService.selectPostListByUserId(getUserId());
            SysDept sysDept = sysDeptService.selectDeptById(getDeptId());
            if (CollectionUtils.isEmpty(userPostList) || ObjectUtils.isEmpty(sysDept)) {
                itemSeizureParam.setInspectUserId(getUserId());
            } else {
                List<SysPost> sysPostList = sysPostService.selectPostAll();
                sysPostList = sysPostList.stream().filter(sysPost -> userPostList.contains(sysPost.getPostId())).collect(Collectors.toList());
                PostTypeEnum typeEnum = sysPostList.stream()
                        .map(item -> {
                            String postCode = item.getPostCode();
                            return postCode != null ? PostTypeEnum.getByCode(postCode) : null;
                        })
                        .filter(java.util.Objects::nonNull)
                        .min(Comparator.comparingInt(PostTypeEnum::getLevel))
                        .orElse(null);
                if (typeEnum == null || typeEnum == PostTypeEnum.security_inspector) {
                    itemSeizureParam.setInspectUserId(getUserId());
                } else {
                    if (sysDept.getDeptType().equals(DeptTypeEnum.STATION.getCode())) {
                        itemSeizureParam.setInspectStationId(sysDept.getDeptId());
                    } else if (sysDept.getDeptType().equals(DeptTypeEnum.BRIGADE.getCode())) {
                        itemSeizureParam.setInspectBrigadeId(sysDept.getDeptId());
                    } else if (sysDept.getDeptType().equals(DeptTypeEnum.MANAGER.getCode())) {
                        itemSeizureParam.setInspectDepartmentId(sysDept.getDeptId());
                    } else if (sysDept.getDeptType().equals(DeptTypeEnum.TEAMS.getCode())) {
                        itemSeizureParam.setInspectTeamId(sysDept.getDeptId());
                    } else {
                        itemSeizureParam.setInspectUserId(getUserId());
                    }
                }
            }
        }
        List<ItemSeizureCount> list = itemSeizureItemsService.selectGroupCount(itemSeizureParam);
        return success(list);
    }

    /**
     * 根据筛选条件查询数量
     */
    @PreAuthorize("@ss.hasPermi('item:items:list')")
    @GetMapping("/selectCount")
    public AjaxResult selectCount(ItemSeizureParam itemSeizureParam) {
        if (itemSeizureParam.getRecordType() == 1) {
            itemSeizureParam.setInspectUserId(getUserId());
        } else {
            itemSeizureParam.setInspectUserId(null);
            List<Long> userPostList = sysPostService.selectPostListByUserId(getUserId());
            SysDept sysDept = sysDeptService.selectDeptById(getDeptId());
            if (CollectionUtils.isEmpty(userPostList) || ObjectUtils.isEmpty(sysDept)) {
                itemSeizureParam.setInspectUserId(getUserId());
            } else {
                if (sysDept.getDeptType().equals(DeptTypeEnum.STATION.getCode())) {
                    itemSeizureParam.setInspectStationId(sysDept.getDeptId());
                } else if (sysDept.getDeptType().equals(DeptTypeEnum.BRIGADE.getCode())) {
                    itemSeizureParam.setInspectBrigadeId(sysDept.getDeptId());
                } else if (sysDept.getDeptType().equals(DeptTypeEnum.MANAGER.getCode())) {
                    itemSeizureParam.setInspectDepartmentId(sysDept.getDeptId());
                } else if (sysDept.getDeptType().equals(DeptTypeEnum.TEAMS.getCode())) {
                    itemSeizureParam.setInspectTeamId(sysDept.getDeptId());
                } else {
                    itemSeizureParam.setInspectUserId(getUserId());
                }
//                List<SysPost> sysPostList = sysPostService.selectPostAll();
//                sysPostList = sysPostList.stream().filter(sysPost -> userPostList.contains(sysPost.getPostId())).collect(Collectors.toList());
//                PostTypeEnum typeEnum = sysPostList.stream()
//                        .map(item -> {
//                            String postCode = item.getPostCode();
//                            return postCode != null ? PostTypeEnum.getByCode(postCode) : null;
//                        })
//                        .filter(java.util.Objects::nonNull)
//                        .min(Comparator.comparingInt(PostTypeEnum::getLevel))
//                        .orElse(null);
//                if (typeEnum == null || typeEnum == PostTypeEnum.security_inspector) {
//                    itemSeizureParam.setInspectUserId(getUserId());
//                } else {
//                    if (sysDept.getDeptType().equals(DeptTypeEnum.STATION.getCode())) {
//                        itemSeizureParam.setInspectStationId(sysDept.getDeptId());
//                    } else if (sysDept.getDeptType().equals(DeptTypeEnumDeptTypeEnumDeptTypeEnumDeptTypeEnum)) {
//                        itemSeizureParam.setInspectDepartmentId(sysDept.getDeptId());
//                    } else if (sysDept.getDeptType().equals(DeptTypeEnum.TEAMS.getCode())) {
//                        itemSeizureParam.setInspectTeamId(sysDept.getDeptId());
//                    } else {
//                        itemSeizureParam.setInspectUserId(getUserId());
//                    }
//                }
            }
        }
        ItemSeizureTotal result = itemSeizureItemsService.selectCount(itemSeizureParam);
        return success(result);
    }
}
