package com.ruoyi.project.ticket.controller;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.ticket.domain.PermitRelation;
import com.ruoyi.project.ticket.domain.TDelayPermit;
import com.ruoyi.project.ticket.domain.THazardWorkPermit;
import com.ruoyi.project.ticket.service.ITDelayPermitService;
import com.ruoyi.project.ticket.service.ITHazardWorkPermitService;
import com.ruoyi.project.ticket.service.ITPermitRelationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 票据列表Controller
 *
 * @author ruoyi
 * @date 2021-10-25
 */
@RestController
@RequestMapping("/ticket/permitRelations")
public class TPermitRelationController extends BaseController {

    @Autowired
    private ITPermitRelationService itPermitRelationService;

    @Autowired
    private ITDelayPermitService itDelayPermitService;

    @Autowired
    private ITHazardWorkPermitService itHazardWorkPermitService;

    /**
     * 查询票据列表
     */
    @GetMapping("/list")
    public TableDataInfo list(PermitRelation permitRelation) {
        startPage();
        List<PermitRelation> list = itPermitRelationService.selectPermitRelations(permitRelation);
        for (PermitRelation relation : list) {
            // 替换工作内容数据
            if (relation.getCbWorkContent() != null) {
                relation.setbWorkContent(relation.getCbWorkContent());
            }
            //TODO 获取延期票次数
            if (relation.getaId() != null) {
                handle(relation, 0);
            }
            if (relation.getcId() != null) {
                handle(relation, 1);
            }
            if (relation.getChildren() != null && relation.getChildren().size() > 0) {
                for (int i = 0; i < relation.getChildren().size(); i++) {
                    if (i == 0) {
                        PermitRelation created = relation.getChildren().get(i);
                        THazardWorkPermit t = new THazardWorkPermit();
                        t.setaId(created.getaId());
                        t.sethVNoOne(Long.parseLong(created.getDelayNo()));
                        this.itHazardWorkPermitService.updateTHazardWorkPermit(t);
                    }
                    if (i == 1) {
                        PermitRelation created = relation.getChildren().get(i);
                        THazardWorkPermit t = new THazardWorkPermit();
                        t.setaId(created.getaId());
                        t.sethVNoTwo(Long.parseLong(created.getDelayNo()));
                        this.itHazardWorkPermitService.updateTHazardWorkPermit(t);
                    }
                    if (i == 2) {
                        PermitRelation created = relation.getChildren().get(i);
                        THazardWorkPermit t = new THazardWorkPermit();
                        t.setaId(created.getaId());
                        t.sethVNoThree(Long.parseLong(created.getDelayNo()));
                        this.itHazardWorkPermitService.updateTHazardWorkPermit(t);
                    }
                }
            }
        }
        return getDataTable(list);
    }

    private void handle(PermitRelation p, int type) {
        TDelayPermit tDelayPermit = new TDelayPermit();
        if (type == 0) {
            tDelayPermit.setaId(p.getaId());
        } else {
            tDelayPermit.setcId(p.getcId());
        }
        List<TDelayPermit> tDelayPermits = this.itDelayPermitService.selectDelayCount(tDelayPermit);
        if (tDelayPermits.size() > 0) {
            p.setDelayCount(tDelayPermits.size() + "");
            List<PermitRelation> children = new ArrayList<>();
            if (tDelayPermits.size() > 10) {
                TDelayPermit t = tDelayPermits.get(0);
                PermitRelation tt1 = new PermitRelation();
                BeanUtils.copyProperties(p, tt1);
                tt1.setDelayCount(t.getvId() + "-5");
                tt1.setDelayNo(t.getvId() + "");
                children.add(tt1);
                TDelayPermit t2 = tDelayPermits.get(5);
                PermitRelation tt2 = new PermitRelation();
                BeanUtils.copyProperties(p, tt2);
                tt2.setDelayCount(t2.getvId() + "-5");
                tt2.setDelayNo(t2.getvId() + "");
                children.add(tt2);
                TDelayPermit t3 = tDelayPermits.get(10);
                PermitRelation tt3 = new PermitRelation();
                BeanUtils.copyProperties(p, tt3);
                tt3.setDelayCount(t3.getvId() + "-" + (tDelayPermits.size() - 10));
                tt3.setDelayNo(t3.getvId() + "");
                children.add(tt3);
            }
            if (tDelayPermits.size() > 5 && tDelayPermits.size() <= 10) {
                TDelayPermit t = tDelayPermits.get(0);
                PermitRelation tt1 = new PermitRelation();
                BeanUtils.copyProperties(p, tt1);
                tt1.setDelayCount(t.getvId() + "-5");
                tt1.setDelayNo(t.getvId() + "");
                children.add(tt1);
                TDelayPermit t2 = tDelayPermits.get(5);
                PermitRelation tt3 = new PermitRelation();
                BeanUtils.copyProperties(p, tt3);
                tt3.setDelayCount(t2.getvId() + "-" + (tDelayPermits.size() - 5));
                tt3.setDelayNo(t2.getvId() + "");
                children.add(tt3);
            }
            if (tDelayPermits.size() <= 5) {
                TDelayPermit t = tDelayPermits.get(0);
                PermitRelation tt2 = new PermitRelation();
                BeanUtils.copyProperties(p, tt2);
                tt2.setDelayCount(t.getvId() + "-" + tDelayPermits.size());
                tt2.setDelayNo(t.getvId() + "");
                children.add(tt2);
            }
            p.setChildren(children);
        } else {
            p.setDelayCount("0");
        }
    }
}
