package com.xy.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.system.SystemUtil;
import com.xy.dto.SysWorkUser.ListDto;
import com.xy.dto.SysWorkUser.SysWorkVo;
import com.xy.entity.DeviceFaultInfoFlow;
import com.xy.error.CommRuntimeException;
import com.xy.mapper.DeviceFaultInfoFlowMapper;
import com.xy.service.DeviceFaultInfoFlowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xy.utils.*;
import io.swagger.annotations.ApiModelProperty;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;

import java.util.List;

import com.xy.dto.DeviceFaultInfoFlowDto;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PostMapping;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.validation.annotation.Validated;

import static com.xy.utils.Beans.copy;
import static com.xy.utils.PlusBeans.toIPage;
import static com.xy.utils.PlusBeans.toPageBean;


/**
* <p>
* 设备故障处理流程 服务实现类
* </p>
*
* @author 谭斌
* @since 2024-01-16
*/
@Service
@RequiredArgsConstructor
@Api(tags = "设备故障处理流程")
public class DeviceFaultInfoFlowServiceImpl extends ServiceImpl<DeviceFaultInfoFlowMapper, DeviceFaultInfoFlow> implements DeviceFaultInfoFlowService {

    private final SysWorkUserService sysWorkUserService;


    @PostMapping("obj")
    @ApiOperation("对象查询")
    public R<DeviceFaultInfoFlowDto.Vo> obj(@RequestBody DeviceFaultInfoFlowDto.Vo vo) {
        DeviceFaultInfoFlowDto.SelectList selectList = copy(DeviceFaultInfoFlowDto.SelectList.class, vo);
        List<DeviceFaultInfoFlowDto.Vo> list = list(selectList).getData();
        if(Emptys.check(list)) {
            return R.ok(list.get(0));
        }
        return R.ok();
    }

    @PostMapping("list")
    @ApiOperation("集合查询")
    public R<List<DeviceFaultInfoFlowDto.Vo>> list(@RequestBody DeviceFaultInfoFlowDto.SelectList selectList) {
        LambdaQueryWrapper<DeviceFaultInfoFlow> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(selectList, DeviceFaultInfoFlow.class).build();
        lambdaQueryWrapper.orderByDesc(DeviceFaultInfoFlow::getCreateTime);
        List<DeviceFaultInfoFlow> list = list(lambdaQueryWrapper);
        return R.ok(copy(DeviceFaultInfoFlowDto.Vo.class, list));
    }

    @PostMapping("page")
    @ApiOperation("分页查询")
    public R<PageBean<DeviceFaultInfoFlowDto.Vo>> page(@RequestBody DeviceFaultInfoFlowDto.Page page) {
        PageBean pageBean = page.getPage();
        LambdaQueryWrapper<DeviceFaultInfoFlow> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(page, DeviceFaultInfoFlow.class).build();
        IPage<DeviceFaultInfoFlow> iPage = page(toIPage(pageBean), lambdaQueryWrapper);
        return R.ok(toPageBean(DeviceFaultInfoFlowDto.Vo.class, iPage));
    }

    @PostMapping("save")
    @ApiOperation("添加")
    public R save(@RequestBody @Validated DeviceFaultInfoFlowDto.Save save) {
        String content = save.getContent();
        if(StrUtil.isEmpty(content)){
            throw new CommRuntimeException("回复内容不可为空");
        }
        DeviceFaultInfoFlow saveInfo = copy(DeviceFaultInfoFlow.class, save);
        Long operatorId = AuthorizeUtils.getLoginId(Long.class);
        Long sysId = AuthorizeUtils.getSysId();
        List<String> roleCodes = AuthorizeUtils.getRoleCodes(sysId);
//        List<Long> roleIds = AuthorizeUtils.getRoleIds(sysId);
        R<List<SysWorkVo>> listR = R.feignCheck(sysWorkUserService.list(new ListDto().setId(CollUtil.newArrayList(operatorId)).setSysId(sysId)));
        List<SysWorkVo> data = listR.getData();
        SysWorkVo sysWorkVo = data.get(0);
        String roleNames = CollUtil.join(sysWorkVo.getRoleNames(), StrUtil.COMMA);
        saveInfo.setOperatorRoleName(roleNames);
        saveInfo.setOperatorRole( CollUtil.join(roleCodes, StrUtil.COMMA));
        saveInfo.setOperatorId( operatorId);
        save(saveInfo);
        return R.ok();
    }

    @PostMapping("update")
    @ApiOperation("修改")
    public R update(@RequestBody @Validated DeviceFaultInfoFlowDto.Update update) {
        DeviceFaultInfoFlow updateInfo = copy(DeviceFaultInfoFlow.class, update);
        updateById(updateInfo);
        return R.ok();
    }
}
