package com.djhk.uav.equ.controller;


import com.djhk.common.annotation.Log;
import com.djhk.common.core.controller.BaseController;
import com.djhk.common.core.domain.AjaxResult;
import com.djhk.common.core.domain.entity.SysDept;
import com.djhk.common.core.domain.entity.SysUser;
import com.djhk.common.core.page.TableDataInfo;
import com.djhk.common.enums.BusinessType;
import com.djhk.common.exception.ServiceException;
import com.djhk.common.utils.SecurityUtils;
import com.djhk.common.utils.poi.ExcelUtil;
import com.djhk.system.service.ISysUserService;
import com.djhk.uav.equ.domain.EquInfoEntity;
import com.djhk.uav.equ.domain.EquModel;
import com.djhk.uav.equ.domain.EquTranEntity;
import com.djhk.uav.equ.domain.dto.EquInfoExportDto;
import com.djhk.uav.equ.domain.vo.EquInfoExportVo;
import com.djhk.uav.equ.service.EquInfoService;
import com.djhk.uav.equ.service.IEquModelService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 设备台账
 *
 * @author gzm
 * @since 1.0.0 2023-06-07
 */
@RestController
@RequestMapping("/equ/info")
@AllArgsConstructor
public class EquInfoController extends BaseController {
    private final EquInfoService equInfoService;

    @Autowired
    private IEquModelService equModelService;

    @Autowired
    private ISysUserService userService;


    @Log(title = "设备台账导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,EquInfoEntity equInfoEntity) {
//        SysUser sysUser = userService.getUserInfoByUserId(SecurityUtils.getUserId(), SecurityConstants.INNER).getData();
        SysUser sysUser = userService.selectUserById(SecurityUtils.getUserId());
        Long deptId = sysUser.getDeptId();
//        equInfoExportVo.setOrgIds(Arrays.asList(ServletUtils.urlDecode(String.valueOf(deptId)).split(",")).stream().map(x -> Long.parseLong(x)).collect(Collectors.toList()));
        equInfoEntity.setDeptId(deptId);
        List<EquInfoEntity> list = equInfoService.selectEquInfoExportList(equInfoEntity);
        ExcelUtil<EquInfoEntity> util = new ExcelUtil<EquInfoEntity>(EquInfoEntity.class);
        util.exportExcel(response, list, "设备台账");
    }

    @Log(title = "设备台账导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        try {
            ExcelUtil<EquInfoExportDto> util = new ExcelUtil<EquInfoExportDto>(EquInfoExportDto.class);
            List<EquInfoExportDto> userList = util.importExcel(file.getInputStream());
            String message = equInfoService.importData(userList, updateSupport);
            return success(message);
        } catch (ServiceException e) {
            return new AjaxResult(e.getCode(),e.getMessage());
        }
    }


    @GetMapping("/list")
    public TableDataInfo list(EquInfoEntity equInfoEntity) {
//        SysUser sysUser = userService.getUserInfoByUserId(SecurityUtils.getUserId(), SecurityConstants.INNER).getData();
        SysUser sysUser = userService.selectUserById(SecurityUtils.getUserId());
        Long deptId = sysUser.getDeptId();
        if (equInfoEntity.getOrganId() == null) {
            equInfoEntity.setOrganId(deptId);
        }
        startPage();
        List<EquInfoEntity> list = equInfoService.getEquInfoList(equInfoEntity);
        //判断设备当前转移状态
        for (EquInfoEntity item : list) {
            List<EquTranEntity> tranSts = equInfoService.getTranStsByEquId(item.getEquId().toString());
            if (tranSts.size() > 0 && tranSts.get(0).getTranSts().equals("0")) {
                String userId = SecurityUtils.getUserId().toString();
                if (userId.equals(tranSts.get(0).getRecvBy())) {
                    item.setTranSts("true");
                } else {
                    item.setTranSts(tranSts.get(0).getTranSts());
                }
            }
        }
        return getDataTable(list);
    }

    @GetMapping("/lists")
//    @RequiresPermissions("uav:equInfo:list")
    public TableDataInfo lists(EquInfoEntity equInfoEntity) {
//        SysUser sysUser = userService.getUserInfoByUserId(SecurityUtils.getUserId(), SecurityConstants.INNER).getData();
        SysUser sysUser = userService.selectUserById(SecurityUtils.getUserId());
        Long deptId = sysUser.getDeptId();
        if (equInfoEntity.getOrganId() == null) {
            equInfoEntity.setOrganId(deptId);
        }
        startPage();
        List<EquInfoEntity> list = equInfoService.getEquInfoLists(equInfoEntity);
//        List<EquInfoEntity> list = new ArrayList<>();
//        EquInfoEntity equInfoEntity1 = new EquInfoEntity();
//        equInfoEntity1.setEquCode("tj002_1001");
//        equInfoEntity1.setModelId("Mavic 30");
//        equInfoEntity1.setMngName("李宁");
//        EquInfoEntity equInfoEntity2 = new EquInfoEntity();
//        equInfoEntity2.setEquCode("tj002_1002");
//        equInfoEntity2.setModelId("DJ HK30");
//        equInfoEntity2.setMngName("张飞");
//        EquInfoEntity equInfoEntity3 = new EquInfoEntity();
//        equInfoEntity3.setEquCode("tj002_1003");
//        equInfoEntity3.setModelId("Matrice 3Y");
//        equInfoEntity3.setMngName("关羽");
//        EquInfoEntity equInfoEntity4 = new EquInfoEntity();
//        equInfoEntity4.setEquCode("tj002_1004");
//        equInfoEntity4.setModelId("Matrice 3T");
//        equInfoEntity4.setMngName("刘备");
//        EquInfoEntity equInfoEntity5= new EquInfoEntity();
//        equInfoEntity5.setEquCode("tj002_1005");
//        equInfoEntity5.setModelId("Matrice 3T");
//        equInfoEntity5.setMngName("刘备");
//        list.add(equInfoEntity1);
//        list.add(equInfoEntity2);
//        list.add(equInfoEntity3);
//        list.add(equInfoEntity4);
//        list.add(equInfoEntity5);
        //判断设备当前转移状态
//        for (EquInfoEntity item : list) {
//            List<EquTranEntity> tranSts = equInfoService.getTranStsByEquId(item.getEquId().toString());
//            if (tranSts.size() > 0 && tranSts.get(0).getTranSts().equals("0")) {
//                String userId = SecurityUtils.getUserId().toString();
//                if (userId.equals(tranSts.get(0).getRecvBy())) {
//                    item.setTranSts("true");
//                } else {
//                    item.setTranSts(tranSts.get(0).getTranSts());
//                }
//            }
//        }
        return getDataTable(list);
  }

    @GetMapping("/{equId}")
    public AjaxResult getInfo(@PathVariable String equId) {
        return AjaxResult.success(equInfoService.selectEquInfoById(equId));
    }

    @PostMapping
    public AjaxResult save(@Validated @RequestBody EquInfoEntity vo) {
        EquInfoEntity equInfoEntity = equInfoService.selectEquInfoByEquSn(vo);
        if (equInfoEntity != null) {
            return new AjaxResult(500, "该组织下设备序列号重复，请先修改序列号");
        }
        Long orgId = vo.getDeptId();
        vo.setOrganId(orgId);
        if (vo.getEquId() == null) {
            vo.setCreateBy(SecurityUtils.getUsername());
            return toAjax(equInfoService.saves(vo));
        } else {
            vo.setUpdateBy(SecurityUtils.getUsername());
            return toAjax(equInfoService.update(vo));
        }
    }


    @PutMapping
    public AjaxResult update(@Validated @RequestBody EquInfoEntity vo) {
        vo.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(equInfoService.update(vo));
    }


    @DeleteMapping("/{equIds}")
    public AjaxResult delete(@PathVariable String[] equIds) {
        //equInfoService.delete(equIds);
        equInfoService.updateEquInfoByIds(equIds);
        return success();
    }



    @GetMapping("/equTranList")
    public TableDataInfo equTranList(EquTranEntity equTranEntity) {
        startPage();
        List<EquTranEntity> list = equInfoService.getEquTranList(equTranEntity);
        return getDataTable(list);
    }

    @PostMapping("/saveEquTran")
    public AjaxResult saveEquTran(@Validated @RequestBody EquTranEntity vo) {
        return toAjax(equInfoService.saveTran(vo));
    }


    @PostMapping("/reviceTran")
    public AjaxResult reviceTran(@Validated @RequestBody Map vo) {
        return toAjax(equInfoService.reciveTranMethod(vo));
    }



    @GetMapping("/getTjTree")
//    @RequiresPermissions("uav:equInfo:query")
    public AjaxResult getTjTree(SysDept dept) {
        return AjaxResult.success(equInfoService.getTjTreeForDept(dept));
    }



    @GetMapping("/getEquModel/{typeId}")
    public List<EquModel> getEquModel(@PathVariable String typeId) {
        EquModel equModel = new EquModel();
        equModel.setTypeId(typeId);
        List<EquModel> equModels = equModelService.selectEquModelList(equModel);
        return equModels;
    }



    @GetMapping("/getTjTrees")
    public List<Object> getTjTrees() {
//        ArrayList<Object> objects = new ArrayList<>();
//        objects.add(equInfoService.getTjTree());

//        return equInfoService.getTjTree();
        List<Object> resultList = new ArrayList<>();
        return resultList;
    }



    @GetMapping("/getMonitorEquList")
    //  @RequiresPermissions("uav:monitorEquInfo:list")
    public AjaxResult getMonitorEquList(@RequestParam("typeIds") String[] typeIds) {
        List<EquInfoEntity> list = equInfoService.selectMonitorEquList(typeIds);
        return AjaxResult.success(list);
    }


}
