package com.foreverwin.mesnac.dispatch.controller;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.foreverwin.mesnac.common.service.UserService;
import com.foreverwin.mesnac.dispatch.dto.UserResourceDto;
import com.foreverwin.mesnac.dispatch.model.UserResource;
import com.foreverwin.mesnac.dispatch.service.UserResourceService;
import com.foreverwin.modular.core.util.CommonMethods;
import com.foreverwin.modular.core.util.FrontPage;
import com.foreverwin.modular.core.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 *
 * @author zjw
 * @since 2021-08-18
 */
@RestController
@RequestMapping("/Z-USER-RESOURCE")
public class UserResourceController {

    @Autowired
    public UserResourceService userResourceService;

    @Autowired
    public UserService userService;

    /**
    * 根据id查询
    *
    * @param id 主键
    * @return
    */
    @ResponseBody
    @GetMapping("/{id:.+}")
    public R getUserResourceById(@PathVariable String id) {
        return R.ok( userResourceService.getById(id));
    }

    /**
     * 查询所有数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("")
    public R getUserResourceList(UserResource userResource){
        List<UserResource> result;
        QueryWrapper<UserResource> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(userResource);
        result = userResourceService.list(queryWrapper);
        return R.ok(result);
    }

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<UserResource> frontPage, UserResource userResource){
        IPage result;
        QueryWrapper<UserResource> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(userResource);
        if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
            //TODO modify global query
            queryWrapper.lambda().and(wrapper -> wrapper
                .like(UserResource::getHandle, frontPage.getGlobalQuery())
                .or().like(UserResource::getSite, frontPage.getGlobalQuery())
                .or().like(UserResource::getResourceBo, frontPage.getGlobalQuery())
                .or().like(UserResource::getResrce, frontPage.getGlobalQuery())
                .or().like(UserResource::getResrceDescription, frontPage.getGlobalQuery())
                .or().like(UserResource::getUserId, frontPage.getGlobalQuery())
                .or().like(UserResource::getUserDescription, frontPage.getGlobalQuery())
                .or().like(UserResource::getTemporaryUser, frontPage.getGlobalQuery())
                .or().like(UserResource::getStatus, frontPage.getGlobalQuery())
                .or().like(UserResource::getCreatedUser, frontPage.getGlobalQuery())
                .or().like(UserResource::getModifiedUser, frontPage.getGlobalQuery())
                .or().like(UserResource::getExistUser, frontPage.getGlobalQuery())
    );
        }
        result = userResourceService.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }

    /**
     * 新增
     * @param userResource  传递的实体
     * @return  null 失败  实体成功
     */
    @PostMapping
    public R save(@RequestBody UserResource userResource) {
        return R.ok(userResourceService.save(userResource));
    }

    /**
     * 修改
     * @param userResource  传递的实体
     * @return  null 失败  实体成功
     */
    @PutMapping
    public R updateById(@RequestBody UserResource userResource) {
        return R.ok(userResourceService.updateById(userResource));
    }

    /**
     * 根据id删除对象
     * @param id  实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id:.+}")
    public R removeById(@PathVariable("id") String id){
        return R.ok(userResourceService.removeById(id));
    }

    /**
     * 批量删除对象
     * @param ids 实体集合ID
     * @return  0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/delete-batch")
    public R removeByIds(List<String> ids){
        return R.ok(userResourceService.removeByIds(ids));
    }

    /**
     * 检索
     * @return
     */
    @ResponseBody
    @GetMapping("/findAll")
    public R findAll(UserResourceDto userResourceDto){
        try {
            return R.ok(userResourceService.findAll(userResourceDto));
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }
    }

    @PostMapping("/saveAll")
    public R saveAll(@RequestBody Map<String,Object> param){
        //查询条件的设备
        String resource = (String) param.get("resource");
        //查询条件的用户
        String user = (String) param.get("user");
        //table列表里面的值
        String str = JSONUtil.toJsonStr(param.get("userResourceList"));
        List<UserResourceDto> userResourceList = JSONArray.parseArray(str, UserResourceDto.class);
        //要删除的数据
        str = JSONUtil.toJsonStr(param.get("deleteTableList"));
        List<UserResource> deleteTableList = JSONArray.parseArray(str, UserResource.class);
        userResourceService.saveAll(userResourceList,deleteTableList);
        return R.ok();
    }

    @ResponseBody
    @GetMapping("/findEmployeeList")
    public R findEmployeeList(String resourceType) {
        try {
            String site = CommonMethods.getSite();
            return R.ok(userResourceService.findEmployeeList(site, resourceType));
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }
    }

    @GetMapping("/findList")
    public R findList(String user){
        return R.ok(userService.findList(user));
    }

    @GetMapping("/findPage")
    public R page(FrontPage frontPage) {
        String globalQuery = frontPage.getGlobalQuery();
        return R.ok(userService.findPage(frontPage.getPagePlus(), globalQuery));
    }

}