package com.jeeplus.test.garbageClearance.controller;

import com.google.common.collect.Lists;
import com.jeeplus.aop.logging.annotation.ApiLog;
import com.jeeplus.notify.service.NotifyService;
import com.jeeplus.notify.service.dto.NotifyDTO;
import com.jeeplus.sys.service.OfficeService;
import com.jeeplus.sys.service.dto.OfficeDTO;
import com.jeeplus.sys.service.dto.UserDTO;
import com.jeeplus.sys.utils.StringUtils;
import com.jeeplus.sys.utils.UserUtils;
import com.jeeplus.test.garbageClearance.domain.LookOver;
import com.jeeplus.test.garbageClearance.service.LookOverService;
import com.jeeplus.test.garbageClearance.utils.SnowFlake;
import com.jeeplus.test.oss.domain.WorkAttachment;
import com.jeeplus.test.oss.domain.WorkAttachmentInfo;
import com.jeeplus.test.oss.service.OssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

/**
 * 巡视相关controller
 * @author 王强
 * @version 1.0
 * @date 2024-08-29 10:40
 */
@Api(tags ="巡视")
@RestController
@RequestMapping(value = "/app/look/over")
public class LookOverController {

    @Autowired
    private LookOverService overService;

    @Autowired
    private OfficeService officeService;

    @Autowired
    private OssService ossService;

    @Autowired
    private NotifyService notifyService;

    /**
     * 保存巡视信息
     */
    @ApiLog("保存巡视信息")
    @ApiOperation(value = "保存巡视信息")
    @PostMapping("save")
    public  ResponseEntity <String> save(@Valid @RequestBody LookOver lookOver) {
        //新增或编辑表单保存
        String lookOverId = SnowFlake.getId();
        UserDTO userDTO = UserUtils.getCurrentUserDTO();
        lookOver.setId(lookOverId);

        if (StringUtils.isNotBlank(lookOver.getNo())) {
            // 剔除掉XJ-J 只保留纯数字
            // 使用正则表达式替换掉所有非数字字符
            String pureNumber = lookOver.getNo().replaceAll("\\D+", "");
            lookOver.setNo(pureNumber);
        }

        if (null != lookOver.getFileList1() && lookOver.getFileList1().size() > 0 ) {
            // 进行附件保存操作，保存到work_attachment 表中
            for (WorkAttachmentInfo attachment : lookOver.getFileList1()) {
                // 生成id
                String detailId = SnowFlake.getId();
                attachment.setId(detailId);
                attachment.setAttachmentId(lookOverId);
                attachment.setCreateTime(new Date());
                attachment.setUpdateTime(new Date());
                attachment.setDelFlag(0);
                ossService.insertWorkAttachmentInfo(attachment,userDTO);
            }
        }

        // 发送通知给 当前用户人所在 村庄的 村庄负责人（村支书）
        UserDTO dto = overService.getUserInfoByOffId(userDTO.getOfficeDTO().getId());

        NotifyDTO notifyDTO = new NotifyDTO();
        notifyDTO.setContent(lookOver.getRemarks());
        notifyDTO.setNotifyRecordIds(dto.getId());
        notifyDTO.setStatus("1");
        notifyDTO.setType("2");
        notifyDTO.setTitle("垃圾清运通知");
        notifyDTO.setLookOverId(lookOverId);

        notifyService.saveOrUpdate ( notifyDTO );


        overService.saveOrUpdate (lookOver);
        return ResponseEntity.ok ( "保存成功" );
    }

    /**
     * 查询最大的巡视工单
     */
    @ApiLog("查询最大的巡视工单")
    @ApiOperation(value = "查询最大的巡视工单")
    @GetMapping("getMaxNo")
    public ResponseEntity<String> getMaxNo() {
        String maxNo = overService.getMaxNo();
        return ResponseEntity.ok( maxNo );
    }

    /**
     * 查询处理单位
     */
    @ApiLog("查询处理单位")
    @ApiOperation(value = "查询处理单位")
    @GetMapping("getProcessingUnit")
    public ResponseEntity<List<OfficeDTO>> getProcessingUnit() {
        List <OfficeDTO> list = officeService.findList ( "" );
        List <OfficeDTO> rootTree = officeService.getRootTree ( "", list, "", "", "1" );

        rootTree.forEach(ro -> {
            Iterator<OfficeDTO> iterator = ro.getChildren().iterator();
            while (iterator.hasNext()) {
                OfficeDTO ch = iterator.next();
                if (!ch.getName().contains("村")) {
                    iterator.remove();  // 使用迭代器删除子节点
                }
            }
        });

        return ResponseEntity.ok ( rootTree );
    }

    /**
     * 根据组织ID获取角色为村负责人的用户信息
     */
    @ApiLog("根据组织ID获取角色为村负责人的用户信息")
    @ApiOperation(value = "根据组织ID获取角色为村负责人的用户信息")
    @GetMapping("getUserInfoByOffId")
    public ResponseEntity<UserDTO> getUserInfo( String officeId ) {
        UserDTO userDTO = overService.getUserInfoByOffId(officeId);
        return ResponseEntity.ok( userDTO );
    }

}
