/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 运维系统
 * 创建日期:2023/11/9
 */
package org.springblade.modules.yjxt.base.servicePerson.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springblade.constant.BusinessConstant;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IDeptService;
import org.springblade.modules.system.service.IUserService;
import org.springblade.modules.yjxt.base.servicePerson.dto.YjxtServicePersonDTO;
import org.springblade.modules.yjxt.base.servicePerson.entity.YjxtServicePersonEntity;
import org.springblade.modules.yjxt.base.servicePerson.service.IYjxtServicePersonServcie;
import org.springblade.modules.yjxt.base.servicePerson.vo.YjxtServicePersonVO;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;


import java.util.List;
import java.util.Objects;

/***
 * Date:2023/11/9
 * Title:文件所属模块（必须填写）
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@RestController
@RequestMapping("galaxy-business/yj/base/serviceperson")
@AllArgsConstructor
@Api(value = "应急系统工作人员管理接口", tags = "增、删、改、查")
public class YjxtServicePersonController extends BladeController {

    private final IYjxtServicePersonServcie yjxtServicePersonServcie;
    private final IUserService userService;
    private final IDeptService deptService;


    @GetMapping("/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "名称", paramType = "query", dataType = "string"),
    })
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "分页")
    public R<IPage<YjxtServicePersonVO>> page(@ApiIgnore YjxtServicePersonDTO dto, Query query) {
        BladeUser user = AuthUtil.getUser();
        IPage<YjxtServicePersonVO> pages = yjxtServicePersonServcie.selectPage(Condition.getPage(query), dto);
        return R.data(pages);
    }

    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "详情", notes = "传入 User")
    public R<YjxtServicePersonEntity> detail(YjxtServicePersonEntity user) {
        YjxtServicePersonEntity detail = yjxtServicePersonServcie.getById(user.getId());
        return R.data(detail);
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增", notes = "传入 User")
    public R save(@RequestBody YjxtServicePersonDTO dto) {
//        LambdaQueryWrapper<User> wrapper = Wrappers.<User>query().lambda();
//        wrapper.eq(User::getAccount, userEntity.getAccount());
//        wrapper.eq(User::getIsDeleted, 0);
//        List<User> list = userService.list(wrapper);
//        if (null != list && list.size() > 0) {
//            return R.fail("帐号已经存在");
//        }
//        userEntity.setName(userEntity.getRealName());
//        userEntity.setPassword(DigestUtil.encrypt(userEntity.getAccount() + BusinessConstant.ACCOUNT_PW_SUFFIX));
//        BladeUser user = AuthUtil.getUser();
//        if (BusinessConstant.ROLE_SYS_ADMIN.equals(user.getRoleName())) {
//            userEntity.setRoleId(BusinessConstant.ROLE_ORG_ADMIN_ID);
//            userEntity.setPostId(BusinessConstant.POST_ADMIN_ID);
//        } else if (BusinessConstant.ROLE_ORG_ADMIN.equals(user.getRoleName())) {
//            userEntity.setRoleId(BusinessConstant.ROLE_COMPANY_ADMIN_ID);
//            userEntity.setPostId(BusinessConstant.POST_ADMIN_ID);
//        } else if (BusinessConstant.ROLE_COMPANY_ADMIN.equals(user.getRoleName())) {
//            userEntity.setRoleId(BusinessConstant.ROLE_COMPANY_SERVICE_PERSON_ID);
//            userEntity.setPostId(BusinessConstant.POST_SERVICE_PERSON_ID);
//            userEntity.setDeptId(user.getDeptId());
//        } else {
//            return R.fail("用户创建错误！");
//        }
//        userEntity.setUserType(1);
        User userEntity = new User();
        userEntity.setAccount(dto.getUserAccount());
        userEntity.setName(dto.getUserName());
        if (userService.save(userEntity)) {
            dto.setUserId(userEntity.getId());
            yjxtServicePersonServcie.save(dto);
        }
        return R.status(true);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改", notes = "传入 User")
    public R update(@RequestBody YjxtServicePersonDTO dto) {
        if (Objects.isNull(dto.getId())) {
            return R.fail("用户ID不能为空！");
        }
        YjxtServicePersonEntity entity = yjxtServicePersonServcie.getById(dto.getId());
        User user = new User();
        user.setId(entity.getUserId());
        user.setName(dto.getUserName());
        return R.status(userService.updateById(user));
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "逻辑删除", notes = "传入 ids")
    public R remove(@ApiParam(value = "主键集合") @RequestParam String ids) {
        List<Long> list = Func.toLongList(ids);
        if (list.size() > 0) {
            for (Long id : list) {
                YjxtServicePersonEntity entity = yjxtServicePersonServcie.getById(id);
                if (userService.removeById(entity.getUserId())) {
                    yjxtServicePersonServcie.removeById(id);
                }
            }
        }
        return R.status(true);
    }

}
