package org.jeecg.modules.apiService.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.bouncycastle.jcajce.provider.symmetric.TEA;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.apiBean.PageBean;
import org.jeecg.modules.apiService.CollectionService;
import org.jeecg.modules.workorderBanner.service.IWorkorderBannerService;
import org.jeecg.modules.workorderCollection.entity.WorkorderCollection;
import org.jeecg.modules.workorderCollection.service.IWorkorderCollectionService;
import org.jeecg.modules.workorderNews.entity.WorkorderNews;
import org.jeecg.modules.workorderStatus.entity.WorkorderStatus;
import org.jeecg.modules.workorderStatus.service.IWorkorderStatusService;
import org.jeecg.modules.workorderTemplate.entity.WorkorderTemplate;
import org.jeecg.modules.workorderTemplate.service.IWorkorderTemplateService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CollectionServiceImpl implements CollectionService {

    //收藏表信息
    @Resource
    private IWorkorderCollectionService workorderCollectionService;

    //工单表信息
    @Resource
    private IWorkorderTemplateService workorderTemplateService;

    //工单状态表信息
    @Resource
    private IWorkorderStatusService workorderStatusService;
    /*************************************************************************************/

    //收藏信息-我的收藏
    @Override
    public Result<?> queryCollectionList(String userId, String statusId, PageBean pageBean) {

        //分页
        Page<WorkorderCollection> page = new Page<WorkorderCollection>(pageBean.getPageNo(), pageBean.getPageSize());
        LambdaQueryChainWrapper<WorkorderCollection> query = workorderCollectionService
                .lambdaQuery();

        //组装查询条件
        if(null != userId){
            //工单状态
            query.eq(WorkorderCollection::getUserId, userId);
        }
        if(null != statusId){
            //工单状态
            query.eq(WorkorderCollection::getStatusId, statusId);
        }

        //获取收藏表信息
        Page<WorkorderCollection> pageList = query.page(page);

        //工单状态列表
        List<WorkorderStatus> workorderStatus = workorderStatusService
                .lambdaQuery()
                .list();

        //将收藏工单信息放入收藏表中
        for (WorkorderCollection record : pageList.getRecords()) {
            WorkorderTemplate template = workorderTemplateService.getById(record.getTemplateId());
            //添加工单状态说明
            for (WorkorderStatus status : workorderStatus) {
                //工单状态为空是，不设置状态描述
                if(null != template.getStatusId()){
                    if(template.getStatusId().equals(status.getId())){
                        template.setStatusDescribe(status.getName());
                        break;
                    }
                }else {
                    continue;
                }

            }
            record.setWorkorderTemplate(template);
        }

        return Result.OK("我的收藏列表", pageList);

    }

    //添加收藏
    @Override
    public Result<?> addCollection(String userId, String templateId) {

        //如果收藏已存在，再次点击则取消收藏
        WorkorderCollection one = workorderCollectionService
                .lambdaQuery()
                .eq(WorkorderCollection::getUserId, userId)
                .eq(WorkorderCollection::getTemplateId, templateId)
                .one();

        if(null != one){
            boolean flag = workorderCollectionService.removeById(one.getId());
            if(flag){
                return Result.OK("取消收藏成功！");
            }else {
                return Result.error("取消收藏失败！");
            }
        }

        //收藏对象
        WorkorderTemplate template = workorderTemplateService
                .lambdaQuery()
                .eq(WorkorderTemplate::getId, templateId)
                .one();

        if(null != template){
            WorkorderCollection collection = new WorkorderCollection();
            collection.setUserId(userId);
            collection.setTemplateId(templateId);
            collection.setStatusId(template.getStatusId());

            //执行添加收藏
            boolean flag = workorderCollectionService.save(collection);
            if(flag){
                return Result.OK("收藏成功！");
            }else {
                return Result.error("收藏失败！");
            }
        }else {
            return Result.error("该工单不存在，收藏失败！");
        }

    }

    //取消收藏
    @Override
    public Result<?> deleteCollection(String userId, String collectonId) {
        //执行添加收藏
        boolean flag = workorderCollectionService.removeById(collectonId);
        if(flag){
            return Result.OK("取消收藏成功！");
        }else {
            return Result.error("取消收藏失败！");
        }
    }
}
