package com.usky.fire.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.core.exception.BusinessException;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.fire.domain.PatrolInspectionContentOption;
import com.usky.fire.domain.PatrolInspectionContent;
import com.usky.fire.domain.PatrolInspectionSiteContent;
import com.usky.fire.mapper.PatrolInspectionContentMapper;
import com.usky.fire.service.PatrolInspectionContentOptionService;
import com.usky.fire.service.PatrolInspectionContentService;
import com.usky.fire.service.PatrolInspectionSiteContentService;
import com.usky.fire.service.util.OnlineMethod;
import com.usky.fire.service.vo.PatrolInspectionContentExportVo;
import com.usky.fire.service.vo.PatrolInspectionContentVo;
import com.usky.system.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author JCB
 * @since 2022-07-15
 */
@Service
public class PatrolInspectionContentServiceImpl extends AbstractCrudService<PatrolInspectionContentMapper, PatrolInspectionContent> implements PatrolInspectionContentService {

    @Autowired
    private PatrolInspectionContentOptionService contentOptionService;
    @Autowired
    private PatrolInspectionSiteContentService siteContentService;

    @Override
    @Transactional
    public void addPatrolInspectionContent(PatrolInspectionContentVo patrolInspectionContentVo) {
        PatrolInspectionContent patrolInspectionContent = new PatrolInspectionContent();
        patrolInspectionContent.setContentTitle(patrolInspectionContentVo.getContentTitle());
        patrolInspectionContent.setContentDescribe(patrolInspectionContentVo.getContentDescribe());
        patrolInspectionContent.setSubmissionMethod(patrolInspectionContentVo.getSubmissionMethod());
        patrolInspectionContent.setTenantId(SecurityUtils.getTenantId());
        patrolInspectionContent.setCompanyId(patrolInspectionContentVo.getCompanyId());
        patrolInspectionContent.setCreateTime(LocalDateTime.now());
        patrolInspectionContent.setCreator(SecurityUtils.getUsername());
        patrolInspectionContent.setEnable(1);
        this.save(patrolInspectionContent);
        if (patrolInspectionContentVo.getContentOptionList().size() > 0) {
            Integer fid = patrolInspectionContent.getId();
            for (int i = 0; i < patrolInspectionContentVo.getContentOptionList().size(); i++) {
                PatrolInspectionContentOption contentOption = new PatrolInspectionContentOption();
                contentOption.setOptionName(patrolInspectionContentVo.getContentOptionList().get(i).getOptionName());
                contentOption.setContentId(fid);
                contentOption.setEnable(1);
                contentOption.setCreator(SecurityUtils.getUsername());
                contentOption.setCreateTime(LocalDateTime.now());
                contentOptionService.save(contentOption);
            }
        }
    }

    @Override
    @Transactional
    public void updatePatrolInspectionContent(PatrolInspectionContentVo patrolInspectionContentVo) {
        LambdaQueryWrapper<PatrolInspectionSiteContent> queryWrapperOne = Wrappers.lambdaQuery();
        queryWrapperOne.eq(PatrolInspectionSiteContent::getContentId, patrolInspectionContentVo.getId());
        List<PatrolInspectionSiteContent> siteContentList = siteContentService.list(queryWrapperOne);
        if (siteContentList.size() > 0) {
            throw new BusinessException("巡检内容已绑定站点不可修改");
        }
        PatrolInspectionContent patrolInspectionContent = new PatrolInspectionContent();
        patrolInspectionContent.setId(patrolInspectionContentVo.getId());
        patrolInspectionContent.setContentTitle(patrolInspectionContentVo.getContentTitle());
        patrolInspectionContent.setContentDescribe(patrolInspectionContentVo.getContentDescribe());
        patrolInspectionContent.setSubmissionMethod(patrolInspectionContentVo.getSubmissionMethod());
        patrolInspectionContent.setCompanyId(patrolInspectionContentVo.getCompanyId());
        this.updateById(patrolInspectionContent);
        LambdaQueryWrapper<PatrolInspectionContentOption> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(PatrolInspectionContentOption::getEnable, 1)
                .eq(PatrolInspectionContentOption::getContentId, patrolInspectionContentVo.getId());
        List<PatrolInspectionContentOption> ContentOptionList = contentOptionService.list(queryWrapper);
        if (ContentOptionList.size() > 0) {
            for (int i = 0; i < ContentOptionList.size(); i++) {
                ContentOptionList.get(i).setEnable(0);
                contentOptionService.updateById(ContentOptionList.get(i));
            }
        }
        if (patrolInspectionContentVo.getContentOptionList().size() > 0) {
            for (int i = 0; i < patrolInspectionContentVo.getContentOptionList().size(); i++) {
                PatrolInspectionContentOption contentOption = new PatrolInspectionContentOption();
                contentOption.setOptionName(patrolInspectionContentVo.getContentOptionList().get(i).getOptionName());
                contentOption.setContentId(patrolInspectionContentVo.getId());
                if (patrolInspectionContentVo.getContentOptionList().get(i).getId() != null && patrolInspectionContentVo.getContentOptionList().get(i).getId() != 0) {
                    contentOption.setId(patrolInspectionContentVo.getContentOptionList().get(i).getId());
                    contentOption.setEnable(1);
                    contentOptionService.updateById(contentOption);
                } else {
                    contentOption.setEnable(1);
                    contentOption.setCreator(SecurityUtils.getUsername());
                    contentOption.setCreateTime(LocalDateTime.now());
                    contentOptionService.save(contentOption);
                }

            }
        }
    }

    @Override
    @Transactional
    public void delPatrolInspectionContent(Integer id) {
        LambdaQueryWrapper<PatrolInspectionSiteContent> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(PatrolInspectionSiteContent::getContentId, id);
        List<PatrolInspectionSiteContent> siteContentList = siteContentService.list(queryWrapper);
        if (siteContentList.size() > 0) {
            throw new BusinessException("巡检内容正在使用中不可删除");
        }
        PatrolInspectionContent patrolInspectionContent = new PatrolInspectionContent();
        patrolInspectionContent.setId(id);
        patrolInspectionContent.setEnable(0);
        this.updateById(patrolInspectionContent);
        LambdaQueryWrapper<PatrolInspectionContentOption> queryWrapperOne = Wrappers.lambdaQuery();
        queryWrapperOne.eq(PatrolInspectionContentOption::getContentId, id)
                .eq(PatrolInspectionContentOption::getEnable, 1);
        List<PatrolInspectionContentOption> contentOptionList = contentOptionService.list(queryWrapperOne);
        if (contentOptionList.size() > 0) {
            for (int i = 0; i < contentOptionList.size(); i++) {
                PatrolInspectionContentOption contentOption = new PatrolInspectionContentOption();
                contentOption.setId(contentOptionList.get(i).getId());
                contentOption.setEnable(0);
                contentOptionService.updateById(contentOption);
            }
        }
    }

    @Override
    public CommonPage<PatrolInspectionContentVo> patrolInspectionContentList(String contentTitle, Integer pageNum, Integer pageSize, Integer id) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String userType = null;
        if (loginUser!=null&&!"".equals(loginUser)){
            userType = loginUser.getUserType();
        }
        LambdaQueryWrapper<PatrolInspectionContent> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(PatrolInspectionContent::getEnable, 1)
                .eq(PatrolInspectionContent::getTenantId, SecurityUtils.getTenantId());
        if ("00".equals(userType)) {
            queryWrapper.eq(PatrolInspectionContent::getCreator, SecurityUtils.getUsername());
        }
        if (contentTitle != null && !"".equals(contentTitle)) {
            queryWrapper.like(PatrolInspectionContent::getContentTitle, contentTitle);
        }
        if (id != 0 && id != null) {
            queryWrapper.eq(PatrolInspectionContent::getId, id);
        }
        Integer total = this.count(queryWrapper);
        queryWrapper.orderByDesc(PatrolInspectionContent::getId);
        if (pageNum != null && pageNum != 0 && pageSize != null && pageSize != 0) {
            Integer startFate = OnlineMethod.getStartFate(pageNum, pageSize);
            queryWrapper.last("limit " + startFate + ","+pageSize);
        }
        List<PatrolInspectionContent> patrolInspectionContentList = this.list(queryWrapper);
//        List<PatrolInspectionContentVo> list = new ArrayList<>();
        List<PatrolInspectionContentVo> patrolInspectionContentVoList = new ArrayList<>();
        if (patrolInspectionContentList.size() > 0) {
            List<Integer> idList = new ArrayList<>();
            for (int i = 0; i < patrolInspectionContentList.size(); i++) {
                idList.add(patrolInspectionContentList.get(i).getId());
            }
            LambdaQueryWrapper<PatrolInspectionContentOption> queryWrapperOne = Wrappers.lambdaQuery();
            queryWrapperOne.eq(PatrolInspectionContentOption::getEnable, 1)
                    .in(PatrolInspectionContentOption::getContentId, idList);
            List<PatrolInspectionContentOption> contentOptionList = contentOptionService.list(queryWrapperOne);
            for (int i = 0; i < patrolInspectionContentList.size(); i++) {
                PatrolInspectionContentVo patrolInspectionContentVo = new PatrolInspectionContentVo();
                patrolInspectionContentVo.setXh(i+1);
                patrolInspectionContentVo.setId(patrolInspectionContentList.get(i).getId());
                patrolInspectionContentVo.setContentTitle(patrolInspectionContentList.get(i).getContentTitle());
                patrolInspectionContentVo.setContentDescribe(patrolInspectionContentList.get(i).getContentDescribe());
                patrolInspectionContentVo.setSubmissionMethod(patrolInspectionContentList.get(i).getSubmissionMethod());
                patrolInspectionContentVo.setTenantId(patrolInspectionContentList.get(i).getTenantId());
                patrolInspectionContentVo.setCompanyId(patrolInspectionContentList.get(i).getCompanyId());
                patrolInspectionContentVo.setCreator(patrolInspectionContentList.get(i).getCreator());
                patrolInspectionContentVo.setCreateTime(patrolInspectionContentList.get(i).getCreateTime());
                patrolInspectionContentVo.setEnable(patrolInspectionContentList.get(i).getEnable());
                List<PatrolInspectionContentOption> contentOptionListOne = new ArrayList<>();
                for (int j = 0; j < contentOptionList.size(); j++) {
                    if (patrolInspectionContentList.get(i).getId() == contentOptionList.get(j).getContentId()) {
                        contentOptionListOne.add(contentOptionList.get(j));
                    }
                }
                patrolInspectionContentVo.setContentOptionList(contentOptionListOne);
                patrolInspectionContentVoList.add(patrolInspectionContentVo);
            }
//            list = patrolInspectionContentVoList.stream().skip((pageNum - 1) * pageSize).limit(pageSize).collect(Collectors.toList());

        }
        return new CommonPage<>(patrolInspectionContentVoList, total, pageSize, pageNum);
    }


    @Override
    public List<PatrolInspectionContentExportVo> contentListExport(String contentTitle, String idList) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String userType = null;
        if (loginUser!=null&&!"".equals(loginUser)){
            userType = loginUser.getUserType();
        }
        LambdaQueryWrapper<PatrolInspectionContent> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(PatrolInspectionContent::getEnable, 1)
                .eq(PatrolInspectionContent::getTenantId, SecurityUtils.getTenantId());
        if ("00".equals(userType)) {
            queryWrapper.eq(PatrolInspectionContent::getCreator, SecurityUtils.getUsername());
        }
        if (contentTitle != null && !"".equals(contentTitle)) {
            queryWrapper.like(PatrolInspectionContent::getContentTitle, contentTitle);
        }
        if (StringUtils.isNotBlank(idList)) {
            List<Integer> listId = new ArrayList<>();
            String[] idArray = idList.split(",");
            for (int i = 0; i < idArray.length; i++) {
                listId.add(Integer.parseInt(idArray[i]));
            }
            queryWrapper.in(PatrolInspectionContent::getId, listId);
        }
        queryWrapper.orderByDesc(PatrolInspectionContent::getId);
        List<PatrolInspectionContent> patrolInspectionContentList = this.list(queryWrapper);
        List<PatrolInspectionContentExportVo> patrolInspectionContentVoList = new ArrayList<>();
        if (patrolInspectionContentList.size() > 0) {
            List<Integer> contentIdList = new ArrayList<>();
            for (int i = 0; i < patrolInspectionContentList.size(); i++) {
                contentIdList.add(patrolInspectionContentList.get(i).getId());
            }
            LambdaQueryWrapper<PatrolInspectionContentOption> queryWrapperOne = Wrappers.lambdaQuery();
            queryWrapperOne.eq(PatrolInspectionContentOption::getEnable, 1)
                    .in(PatrolInspectionContentOption::getContentId, contentIdList);
            List<PatrolInspectionContentOption> contentOptionList = contentOptionService.list(queryWrapperOne);
            for (int i = 0; i < patrolInspectionContentList.size(); i++) {
                PatrolInspectionContentExportVo patrolInspectionContentVo = new PatrolInspectionContentExportVo();
                patrolInspectionContentVo.setXh(i+1);
                patrolInspectionContentVo.setId(patrolInspectionContentList.get(i).getId());
                patrolInspectionContentVo.setContentTitle(patrolInspectionContentList.get(i).getContentTitle());
                patrolInspectionContentVo.setContentDescribe(patrolInspectionContentList.get(i).getContentDescribe());
                patrolInspectionContentVo.setSubmissionMethod(patrolInspectionContentList.get(i).getSubmissionMethod());
                patrolInspectionContentVo.setTenantId(patrolInspectionContentList.get(i).getTenantId());
                patrolInspectionContentVo.setCompanyId(patrolInspectionContentList.get(i).getCompanyId());
                patrolInspectionContentVo.setCreator(patrolInspectionContentList.get(i).getCreator());
                patrolInspectionContentVo.setCreateTime(patrolInspectionContentList.get(i).getCreateTime().format(df));
                patrolInspectionContentVo.setEnable(patrolInspectionContentList.get(i).getEnable());
                List<PatrolInspectionContentOption> contentOptionListOne = new ArrayList<>();
                for (int j = 0; j < contentOptionList.size(); j++) {
                    if (patrolInspectionContentList.get(i).getId() == contentOptionList.get(j).getContentId()) {
                        contentOptionListOne.add(contentOptionList.get(j));
                    }
                }
                patrolInspectionContentVo.setContentOptionList(contentOptionListOne);
                patrolInspectionContentVoList.add(patrolInspectionContentVo);
            }
        }
        return patrolInspectionContentVoList;
    }


}
