package com.zbf.web.controller.system;

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

import com.github.pagehelper.util.StringUtil;
import com.zbf.common.core.domain.TreeSelect;
import com.zbf.common.core.domain.entity.SysDept;
import com.zbf.common.core.domain.entity.SysUser;
import com.zbf.system.domain.TMiStock;
import com.zbf.system.service.ISysDeptService;
import com.zbf.system.service.ISysUserService;
import com.zbf.web.controller.section.EnhanceDataList;
import org.apache.commons.lang3.ObjectUtils;
import org.dom4j.util.StringUtils;
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.zbf.common.annotation.Log;
import com.zbf.common.core.controller.BaseController;
import com.zbf.common.core.domain.AjaxResult;
import com.zbf.common.enums.BusinessType;
import com.zbf.system.domain.TckOrderdetailBak;
import com.zbf.system.service.ITckOrderdetailBakService;
import com.zbf.common.utils.poi.ExcelUtil;
import com.zbf.common.core.page.TableDataInfo;

/**
 * 出库物料明细Controller
 *
 * @author tony
 * @date 2024-09-09
 */
@RestController
@RequestMapping("/system/tckOrderDetailbak")
public class TckOrderdetailBakController extends BaseController
{
    @Autowired
    private ITckOrderdetailBakService tckOrderdetailBakService;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private ISysUserService sysUserService;

/**
 * 查询出库物料明细列表
 */
@GetMapping("/list")
@EnhanceDataList(entityType = TckOrderdetailBak.class)
    public TableDataInfo list(TckOrderdetailBak tckOrderdetailBak)
    {
        startPage();
        if (tckOrderdetailBak.getDate()!=null){
            String[] date = tckOrderdetailBak.getDate();
            tckOrderdetailBak.setStartTime(date[0]);
            tckOrderdetailBak.setEndTime(date[1]);
        }
        if(StringUtil.isNotEmpty(tckOrderdetailBak.getSupplier())){
            //查找该部门下对应的子部门
            SysDept sysDept = new SysDept();
            sysDept.setDeptName(tckOrderdetailBak.getSupplier());
            List<SysDept> sysDepts = sysDeptService.selectDeptList(sysDept);
            String deptId = String.valueOf(sysDepts.get(0).getDeptId());
            List<SysDept> treeSelects = sysDeptService.selectDeptList(new SysDept());
            List<SysDept> collect = treeSelects.stream()
                    .filter(f -> Arrays.asList(f.getAncestors().split(",")).contains(String.valueOf(deptId))
                    || f.getDeptId() == sysDepts.get(0).getDeptId() )
                    .collect(Collectors.toList());
            Long[] ids = collect.stream()
                    .map(SysDept::getDeptId) // 提取 ID
                    .toArray(Long[]::new);

            //查询该部门下的所有员工
            List<SysUser> sysUsers = sysUserService.selectUserByDeptIds(ids);
            String[] nickName = sysUsers.stream().map(SysUser::getNickName).toArray(String[]::new);
            if(StringUtil.isNotEmpty(tckOrderdetailBak.getApplicant())){
                if (Arrays.asList(nickName).contains(tckOrderdetailBak.getApplicant())) {
                    // 包含
                    return getDataTable(tckOrderdetailBakService.selectTckOrderdetailBakList(tckOrderdetailBak));
                }else{
                    TckOrderdetailBak tckOrderdetailBak1 = new TckOrderdetailBak();
                    tckOrderdetailBak1.setApplicant("不存在");
                    return getDataTable(tckOrderdetailBakService.selectTckOrderdetailBakList(tckOrderdetailBak1));
                }
            }
            List<TckOrderdetailBak> list = tckOrderdetailBakService.selectTckOrderdetailBakList(new TckOrderdetailBak());
            //查找表中符合的员工对应的数据
            List<TckOrderdetailBak> filteredList = list.stream()
                    .filter(f -> StringUtil.isNotEmpty(f.getApplicant())&&
                            Arrays.stream(nickName)
                                    .anyMatch(n -> f.getApplicant().contains(n)))
                    .collect(Collectors.toList());
            return getDataTable(filteredList);
        }else{
            List<TckOrderdetailBak> list = tckOrderdetailBakService.selectTckOrderdetailBakList(tckOrderdetailBak);
            for (TckOrderdetailBak t : list) {
                SysUser sysUser = new SysUser();
                sysUser.setNickName(t.getApplicant());
                List<SysUser> sysUsers = sysUserService.selectUserList(sysUser);
                if(ObjectUtils.isEmpty(sysUsers)){
                    t.setSupplier("");
                }else{
                    Long deptId = sysUsers.get(0).getDeptId();
                    SysDept sysDept = new SysDept();
                    sysDept.setDeptId(deptId);
                    List<SysDept> sysDepts = sysDeptService.selectDeptList(sysDept);
                    if(ObjectUtils.isEmpty(sysDepts)){
                        t.setSupplier("");
                    }else{
                        SysDept sysDept1 = sysDepts.get(0);
                        if(sysDept1.getAncestors().split(",").length>3){
                            String s = sysDept1.getAncestors().split(",")[2];
                            sysDept.setDeptId(Long.valueOf(s));
                            List<SysDept> sysDepts1 = sysDeptService.selectDeptList(sysDept);
                            t.setSupplier(sysDepts1.get(0).getDeptName());
                        }else{
                            t.setSupplier(sysDept1.getDeptName());
                        }
                    }
                }
            }
            return getDataTable(list);
        }

    }

//    @GetMapping("/supplier")
//    @EnhanceDataList(entityType = TckOrderdetailBak.class)
//    public TableDataInfo supplier()
//    {
//        TckOrderdetailBak tckOrderdetailBak = new TckOrderdetailBak();
//        List<TckOrderdetailBak> list = tckOrderdetailBakService.selectTckOrderdetailBakList(tckOrderdetailBak);
//        return getDataTable(list);
//    }

    @GetMapping("/lists")
    @EnhanceDataList(entityType = TMiStock.class)
    public AjaxResult lists(TckOrderdetailBak tckOrderdetailBak) {
        if (tckOrderdetailBak.getDate()!=null){
            String[] date = tckOrderdetailBak.getDate();
            tckOrderdetailBak.setStartTime(date[0]);
            tckOrderdetailBak.setEndTime(date[1]);
        }
        List<TckOrderdetailBak> list = tckOrderdetailBakService.select(tckOrderdetailBak);
        return AjaxResult.success(list);
    }

    /**
     * 导出出库物料明细列表
     */
//    @PreAuthorize("@ss.hasPermi('system:tckOrderDetailbak:export')")
    @Log(title = "出库物料明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TckOrderdetailBak tckOrderdetailBak)
    {
        List<TckOrderdetailBak> list = tckOrderdetailBakService.selectTckOrderdetailBakList(tckOrderdetailBak);
        ExcelUtil<TckOrderdetailBak> util = new ExcelUtil<TckOrderdetailBak>(TckOrderdetailBak.class);
        util.exportExcel(response, list, "出库物料明细数据");
    }

    /**
     * 获取出库物料明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:tckOrderDetailbak:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(tckOrderdetailBakService.selectTckOrderdetailBakById(id));
    }

    /**
     * 新增出库物料明细
     */
    @PreAuthorize("@ss.hasPermi('system:tckOrderDetailbak:add')")
    @Log(title = "出库物料明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TckOrderdetailBak tckOrderdetailBak)
    {
        return toAjax(tckOrderdetailBakService.insertTckOrderdetailBak(tckOrderdetailBak));
    }

    /**
     * 修改出库物料明细
     */
    @PreAuthorize("@ss.hasPermi('system:tckOrderDetailbak:edit')")
    @Log(title = "出库物料明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TckOrderdetailBak tckOrderdetailBak)
    {
        return toAjax(tckOrderdetailBakService.updateTckOrderdetailBak(tckOrderdetailBak));
    }

    /**
     * 删除出库物料明细
     */
    @PreAuthorize("@ss.hasPermi('system:tckOrderDetailbak:remove')")
    @Log(title = "出库物料明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(tckOrderdetailBakService.deleteTckOrderdetailBakByIds(ids));
    }

    /**
     * 启用
     */
    @PreAuthorize("@ss.hasPermi('system:tckOrderDetailbak:remove')")
    @Log(title = "出库物料明细", businessType = BusinessType.UPDATE)
    @DeleteMapping("/disable/{ids}")
    public AjaxResult disable(@PathVariable String[] ids)
    {
        return toAjax(tckOrderdetailBakService.disabledTckOrderdetailBakByIds(ids));
    }

    /**
     * 禁用
     */
    @PreAuthorize("@ss.hasPermi('system:tckOrderDetailbak:edit')")
    @Log(title = "出库物料明细", businessType = BusinessType.UPDATE)
    @DeleteMapping("/enable/{ids}")
    public AjaxResult enable(@PathVariable String[] ids)
    {
        return toAjax(tckOrderdetailBakService.enableTckOrderdetailBakByIds(ids));
    }

    /**
     * 修改状态
     */
    @PreAuthorize("@ss.hasPermi('system:tckOrderDetailbak:edit')")
    @Log(title = "出库物料明细", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody TckOrderdetailBak tckOrderdetailBak) {
        return toAjax(tckOrderdetailBakService.updateStatus(tckOrderdetailBak));
    }

}

