package com.djhk.uav.warLawLibrary.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.model.LoginUser;
import com.djhk.common.core.page.TableDataInfo;
import com.djhk.common.core.redis.RedisCache;
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.uav.warLawLibrary.domain.WarLawLibrary;
import com.djhk.uav.warLawLibrary.domain.WarLawLibraryVO;
import com.djhk.uav.warLawLibrary.service.IWarLawLibraryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * com.djhk.uav.warLawLibrary.domain.WarLawLibraryController
 * 
 * @author ruoyi
 * @date 2024-11-15
 */
@RestController
@RequestMapping("/warLawLibrary")
public class WarLawLibraryController extends BaseController
{
    @Autowired
    private IWarLawLibraryService warLawLibraryService;

    @Autowired
    private RedisCache redisService;

    /**
     * 查询com.djhk.uav.warLawLibrary.domain.WarLawLibrary列表
     */
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody WarLawLibrary warLawLibrary)
    {
        WarLawLibrary lawLibrary = redisService.getCacheObject("war_law_library" + warLawLibrary.getWarName());
        if (null != lawLibrary)
        {
            ArrayList<WarLawLibrary> o = new ArrayList<>();
            o.add(lawLibrary);
            return   getDataTable(o);
        }
//        LoginUser loginUser = SecurityUtils.getLoginUser();
//        if (null == loginUser) {
//            throw new ServiceException("登录失效，请重新登录！", 500);
//        }
//        Long deptId = loginUser.getSysUser().getDeptId();
//        warLawLibrary.setDeptId(deptId);
//        startPage();
//        List<WarLawLibrary> list = warLawLibraryService.selectWarLawLibraryList(warLawLibrary);
//        String str = list.get(0).getWarContent();
//        str = str.replace("=", ":").replace("[", "{\"value\":[").replace("],", "],").replace("]", "]}");
//        str = str.replace("name", "\"name\"");
//        JSONArray jsonArray = new JSONArray(str);
//        List<JSONArray> out = Arrays.asList(jsonArray);
//        list.get(0).setWarContentList(Arrays.asList(out));
//        return getDataTable(list);
        return null;
    }

    @GetMapping("/warNameList")
    public TableDataInfo warNameList(WarLawLibrary warLawLibrary)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (null == loginUser) {
            throw new ServiceException("登录失效，请重新登录！", 500);
        }
        Long deptId = SecurityUtils.getDeptId();
        warLawLibrary.setDeptId(deptId);
        startPage();
        List<WarLawLibrary> list = warLawLibraryService.selectWarLawLibraryList(warLawLibrary);
        List<WarLawLibraryVO> out = new ArrayList<>();
        for (WarLawLibrary lawLibrary : list) {
            WarLawLibraryVO vo = new WarLawLibraryVO();
            vo.setWarName(lawLibrary.getWarName());
            out.add(vo);
        }
//        List<String> collect = list.stream().map(WarLawLibrary::getWarName).collect(Collectors.toList());
//        Map<Object, Object> map = new HashMap<>();
//        map.put("warName",collect);
        return getDataTable(out);
    }

    /**
     * 导出com.djhk.uav.warLawLibrary.domain.WarLawLibrary列表
     */
    @Log(title = "com.djhk.uav.warLawLibrary.domain.WarLawLibrary", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WarLawLibrary warLawLibrary)
    {
        List<WarLawLibrary> list = warLawLibraryService.selectWarLawLibraryList(warLawLibrary);
        ExcelUtil<WarLawLibrary> util = new ExcelUtil<WarLawLibrary>(WarLawLibrary.class);
        util.exportExcel(response, list, "com.djhk.uav.warLawLibrary.domain.WarLawLibrary数据");
    }

//    /**
//     * 获取com.djhk.uav.warLawLibrary.domain.WarLawLibrary详细信息
//     */
//    @GetMapping(value = "/{id}")
//    public AjaxResult getInfo(@PathVariable("id") Long id)
//    {
//        return success(warLawLibraryService.selectWarLawLibraryById(id));
//    }
    /**
     * 新增com.djhk.uav.warLawLibrary.domain.WarLawLibrary
     */
    @Log(title = "com.djhk.uav.warLawLibrary.domain.WarLawLibrary", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WarLawLibrary warLawLibrary)
    {
        Date date = new Date();
        String d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
        WarLawLibrary lawLibrary = new WarLawLibrary();
        lawLibrary.setWarName(warLawLibrary.getWarName());
        List<WarLawLibrary> list = warLawLibraryService.selectWarLawLibraryList(lawLibrary);
        if (list.size() != 0){
//            throw new ServiceException("该战法已存在！");
            warLawLibrary.setWarContent(warLawLibrary.getWarContentList().toString());
            warLawLibrary.setId(list.get(0).getId());
            return toAjax(warLawLibraryService.updateWarLawLibrary(warLawLibrary));
        }else {
            LoginUser loginUser = SecurityUtils.getLoginUser();
            if (null == loginUser) {
                throw new ServiceException("登录失效，请重新登录！", 500);
            }
//            Long deptId = loginUser.getSysUser().getDeptId();
            Long deptId = SecurityUtils.getDeptId();
            warLawLibrary.setDeptId(deptId);
            warLawLibrary.setWarName(warLawLibrary.getWarName() + d);
            warLawLibrary.setWarContent(warLawLibrary.getWarContentList().toString());
            return toAjax(warLawLibraryService.insertWarLawLibrary(warLawLibrary));
        }
    }

    /**
     * 修改com.djhk.uav.warLawLibrary.domain.WarLawLibrary
     */
    @Log(title = "com.djhk.uav.warLawLibrary.domain.WarLawLibrary", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WarLawLibrary warLawLibrary)
    {
        return toAjax(warLawLibraryService.updateWarLawLibrary(warLawLibrary));
    }

    /**
     * 删除com.djhk.uav.warLawLibrary.domain.WarLawLibrary
     */
    @Log(title = "com.djhk.uav.warLawLibrary.domain.WarLawLibrary", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(warLawLibraryService.deleteWarLawLibraryById(id));
    }
    /**
     * 删除com.djhk.uav.warLawLibrary.domain.WarLawLibrary
     */
    @Log(title = "com.djhk.uav.warLawLibrary.domain.WarLawLibrary", businessType = BusinessType.DELETE)
	@GetMapping("/removeByWarName")
    public AjaxResult removeByWarName(@RequestParam String warName)
    {
        return toAjax(warLawLibraryService.removeByWarName(warName));
    }
}
