/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.manager.controller;

import com.alibaba.nacos.common.utils.MD5Utils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;

import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.manager.dto.FindAllUserByConditionDTO;
import org.springblade.manager.dto.SaveUserInfoByProjectDTO;
import org.springblade.manager.dto.WbsTreeContractDTO;
import org.springblade.manager.service.IWbsTreeContractService;
import org.springblade.manager.service.SaveUserInfoByProjectService;
import org.springblade.manager.vo.CRolePostVO;
import org.springblade.manager.vo.WbsTreeContractTreeVO;
import org.springblade.manager.vo.WbsTreeVO;
import org.springblade.system.user.entity.User;
import org.springblade.system.user.feign.IUserClient;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;

import org.springblade.manager.entity.ContractInfo;
import org.springblade.manager.vo.ContractInfoVO;
import org.springblade.manager.wrapper.ContractInfoWrapper;
import org.springblade.manager.service.IContractInfoService;
import org.springblade.core.boot.ctrl.BladeController;
import sun.security.provider.MD5;
import sun.security.provider.SHA;

import java.util.*;

/**
 * 合同段信息表 控制器
 *
 * @author liuyc
 * @since 2022-04-14
 */
@RestController
@AllArgsConstructor
@RequestMapping("/contractInfo")
@Api(value = "合同段", tags = "合同段接口")
public class ContractInfoController extends BladeController {

    private final IContractInfoService contractInfoService;
    private final SaveUserInfoByProjectService saveUserInfoByProject;
    private final IUserClient iUserClient;
    private final IWbsTreeContractService wbsTreeContractService;

    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入contractInfo")
    public R<ContractInfoVO> detail(ContractInfo contractInfo) {
        ContractInfo detail = contractInfoService.getOne(Condition.getQueryWrapper(contractInfo));
        return R.data(ContractInfoWrapper.build().entityVO(detail));
    }

    /**
     * 根据项目名称查询当前所有合同
     */
    @GetMapping("/findContractInfoByPid")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "根据项目id查询当前所有合同", notes = "传入pid")
    public R<List<ContractInfo>> findContractInfoByPid(String pid) {
        List<ContractInfo> contractInfoVOS = contractInfoService.selectContractInfoPageByPid(pid);
        return R.data(contractInfoVOS);
    }

    /**
     * 分页 合同段信息表
     */
    @GetMapping("/list")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页", notes = "传入contractInfo")
    public R<IPage<ContractInfoVO>> list(ContractInfo contractInfo, Query query) {
        IPage<ContractInfo> pages = contractInfoService.page(Condition.getPage(query), Condition.getQueryWrapper(contractInfo));
        return R.data(ContractInfoWrapper.build().pageVO(pages));
    }

    /**
     * 自定义分页 合同段信息表
     */
    @GetMapping("/page")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "自定义分页", notes = "传入contractInfo")
    public R<IPage<ContractInfoVO>> page(ContractInfoVO contractInfo, Query query) {
        IPage<ContractInfoVO> pages = contractInfoService.selectContractInfoPage(Condition.getPage(query), contractInfo);
        return R.data(pages);
    }


    /**
     * 新增 合同段信息表
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增", notes = "传入contractInfo")
    public R save(@Valid @RequestBody ContractInfo contractInfo) {
        return R.status(contractInfoService.save(contractInfo));
    }

    /**
     * 修改 合同段信息表
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改", notes = "传入contractInfo")
    public R update(@Valid @RequestBody ContractInfo contractInfo) {
        return R.status(contractInfoService.updateById(contractInfo));
    }

    /**
     * 删除文件
     */
    @GetMapping("/deleteFile")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "删除文件", notes = "传入文件url")
    public R deleteFile(@Valid @RequestParam("url") String url) {
        return R.status(contractInfoService.deleteFile(url));
    }


    /**
     * 新增或修改 合同段信息表
     */
    @PostMapping("/submit")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "新增或修改", notes = "传入contractInfo")
    public R submit(@Valid @RequestBody ContractInfoVO contractInfo) {
        //新增
        boolean b = contractInfoService.saveOrUpdate(contractInfo);
        if (b) {
            return R.data(contractInfo, "操作成功");
        }
        return R.fail("操作失败");
    }


    /**
     * 删除 合同段信息表
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "逻辑删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return R.status(contractInfoService.deleteLogic(Func.toLongList(ids)));
    }


    /**
     * 查询角色的岗位
     */
    @GetMapping("/findJobByRoleId")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "查询角色下的岗位", notes = "传入id")
    public R<List<CRolePostVO>> findJobByRoleId(@ApiParam(value = "id", required = true)
                                                @RequestParam("id") String id) {
        List<CRolePostVO> list = contractInfoService.findJobByRoleId(id);
        if (list.size() <= 0) {
            return R.fail("未查询到信息");
        }
        return R.data(list);
    }

    /**
     * 根据项目id、合同id、角色id、岗位id查询当前分配的用户
     */
    @PostMapping("/findAllUserByCondition")
    @ApiOperationSupport(order = 9)
    @ApiOperation(value = "查询当前项目合同角色岗位下的用户", notes = "传入项目id、合同id、角色id、岗位id")
    public R<List<User>> findAllUserByCondition(@RequestBody FindAllUserByConditionDTO values) {
        List<User> users = contractInfoService.findAllUserByCondition(values);
        //密码明文解码
        /*for (User user : users) {
            String password = user.getPassword();
            DigestUtils.md5DigestAsHex(password.getBytes());
        }*/
        if (users.size() > 0) {
            return R.data(users);
        }
        return R.data(users, "未查询到数据");
    }

    /**
     * 根据项目id、合同id、角色id查询当前运维的用户
     */
    @PostMapping("/findUserListByCondition")
    @ApiOperationSupport(order = 9)
    @ApiOperation(value = "查询当前项目角色下的运维用户列表", notes = "传入项目id、角色id、合同id(非必传)")
    public R<List<User>> findUserListByCondition(@RequestBody FindAllUserByConditionDTO values) {
        List<User> users = contractInfoService.findUserListByCondition(values);
        if (users.size() > 0) {
            return R.data(users);
        }
        return R.data(users, "未查询到数据");
    }


    /**
     * 根据姓名查询用户
     */
    @GetMapping("/findUserByName")
    @ApiOperationSupport(order = 14)
    @ApiOperation(value = "根据姓名模糊查询用户", notes = "传入用户姓名")
    public R<List<User>> findUserByName(@RequestParam("name") String name) {
        List<User> userByName = contractInfoService.findUserByName(name);
        if (userByName.size() > 0) {
            return R.data(userByName);
        }
        return R.success("未查询到信息");
    }


    /**
     * 项目分配用户批量保存
     */
    @PostMapping("/saveUserInfoByProject")
    @ApiOperationSupport(order = 11)
    @ApiOperation(value = "保存当前用户与合同段关联信息", notes = "传入项目id、合同id、角色id、岗位id、用户id List集合")
    public R saveUserInfoByProject(@RequestBody List<SaveUserInfoByProjectDTO> list) {
//        list.stream().forEach(System.out::print);
        if (list.size() > 0) {
            Boolean result = saveUserInfoByProject.saveBatch(list);
            if (result) {
                return R.success("保存成功");
            }
        }
        return R.fail("保存失败");
    }

    /**
     * 项目分配用户逻辑删除
     */
    @PostMapping("/removeUsersByIds")
    @ApiOperationSupport(order = 12)
    @ApiOperation(value = "逻辑删除", notes = "传入当前列表用户的记录ids")
    public R removeUsersByIds(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return R.status(saveUserInfoByProject.deleteLogic(Func.toLongList(ids)));
    }

    /**
     * 重置密码
     */
    @PostMapping("/resetPasswordByUserId")
    @ApiOperationSupport(order = 13)
    @ApiOperation(value = "重置密码", notes = "传入当前用户的userId，默认重置密码123456")
    public R updatePasswordByUserId(@ApiParam(value = "用户userId", required = true) @RequestParam String userId) {
        if (StringUtils.isNotEmpty(userId)) {
            return iUserClient.resetPassword(userId);
        }
        return R.fail("操作失败");
    }


    /**
     * 分配合同段wbs节点树保存或者修改
     */
    @PostMapping("/submitWbsTreeInContract")
    @ApiOperationSupport(order = 13)
    @ApiOperation(value = "分配合同段wbs节点树保存或修改", notes = "传入ids,wbsId,项目id,合同段id")
    public R submitWbsTreeInContract(@RequestBody WbsTreeContractDTO wbsTreeContractDTO) {
        Boolean b = wbsTreeContractService.submitWbsTreeInContract(wbsTreeContractDTO);
        if (b) {
            return R.success("操作成功");
        }
        return R.fail("操作失败");
    }

    /**
     * 查询合同段私有Wbs节点树形结构
     */
    @GetMapping("/tree")
    @ApiOperationSupport(order = 14)
    @ApiOperation(value = "查询合同段私有Wbs节点树形结构", notes = "传入wbsId,项目id,合同段id")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "wbsId", value = "wbsId", required = true),
            @ApiImplicitParam(name = "项目id", value = "projectId", required = true),
            @ApiImplicitParam(name = "合同段id", value = "contractId", required = true)
    })
    public R saveWbsTreeInContract(String wbsId, String projectId, String contractId) {
        List<WbsTreeContractTreeVO> tree = contractInfoService.tree(wbsId, projectId, contractId);
        if (tree != null && tree.size() > 0) {
            return R.data(tree);
        }
        return R.fail(200, "未查询到信息");
    }


}
