package com.ai.da.service.impl;

import com.ai.da.common.config.exception.BusinessException;
import com.ai.da.common.enums.LayersPriorityEnum;
import com.ai.da.mapper.primary.DesignItemDetailCanvasMapper;
import com.ai.da.mapper.primary.DesignItemDetailMapper;
import com.ai.da.mapper.primary.entity.DesignItemDetail;
import com.ai.da.mapper.primary.entity.DesignItemDetailCanvas;
import com.ai.da.service.DesignItemDetailService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import static com.ai.da.common.enums.LayersPriorityEnum.BODY;

/**
 * 服务实现类
 *
 * @author yanglei
 * @since 2022-09-30
 */
@Slf4j
@Service
public class DesignItemDetailServiceImpl extends ServiceImpl<DesignItemDetailMapper, DesignItemDetail> implements DesignItemDetailService {
    @Resource
    private DesignItemDetailMapper designItemDetailMapper;
    @Resource
    private DesignItemDetailCanvasMapper designItemDetailCanvasMapper;


    @Override
    public int deleteByDesignId(Long designId) {
//        QueryWrapper<DesignItemDetail> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("design_id", designId);
//
//        DesignItemDetail itemDetail = new DesignItemDetail();
//        itemDetail.setDesignId(0L);
//        return designItemDetailMapper.update(itemDetail,queryWrapper);
        //不删除 跟design走
        return 1;
    }

    @Override
    public int deleteByDesignItemId(Long designItemId) {
//        QueryWrapper<DesignItemDetail> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("design_item_id", designItemId);
//        DesignItemDetail designItemDetail = new DesignItemDetail();
//        designItemDetail.setIsDeleted((byte)1);

        UpdateWrapper<DesignItemDetail> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("design_item_id", designItemId);
        updateWrapper.set("is_deleted", (byte) 1);
        return designItemDetailMapper.update(null, updateWrapper);
    }

    @Override
    public List<DesignItemDetail> selectByDesignItemId(Long designItemId) {
        QueryWrapper<DesignItemDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("design_item_id", designItemId);
        queryWrapper.eq("is_deleted", 0);
        return designItemDetailMapper.selectList(queryWrapper);
    }

    private DesignItemDetail selectById(Long id) {
        QueryWrapper<DesignItemDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        return designItemDetailMapper.selectOne(queryWrapper);
    }

    private Boolean exists(Long id) {
        QueryWrapper<DesignItemDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        return designItemDetailMapper.exists(queryWrapper);
    }

    private boolean saveOne(DesignItemDetail collectionElement) {
        if (designItemDetailMapper.insert(collectionElement) <= 0) {
            throw new BusinessException("save.designItemDetail.failed");
        }
        return Boolean.TRUE;
    }


    /**
     * 旧系统不允许添加多件衣服，衣服图层没有优先级，
     * 升级为可以添加多件衣服后，衣服图层必须要有优先级，故出现不兼容的情况
     * 在这里为没有优先级的衣服添加默认优先级
     * @param designItemDetailList
     * @return
     */
    @Override
    public void setDesignItemDetailPriority(List<DesignItemDetail> designItemDetailList){
        for (DesignItemDetail detail:designItemDetailList){
            if (!detail.getType().equals(BODY.getType()) && detail.getPriority().equals(BODY.getValue())){
                detail.setPriority(LayersPriorityEnum.getValueByType(detail.getType()).getValue());
            }
        }
    }

    @Override
    public void saveDesignItemDetailCanvas(Long designItemDetailId, Long exportFileId){
        // 1. 每件单品只会有一张画布信息
        DesignItemDetailCanvas didc = getDIDCByDesignItemDetailId(designItemDetailId);
        // 2. 没有，则新增一条
        if (Objects.isNull(didc)){
            didc = new DesignItemDetailCanvas();
            didc.setDesignItemDetailId(designItemDetailId);
            didc.setExportFileId(exportFileId);
            didc.setCreateTime(LocalDateTime.now());
            designItemDetailCanvasMapper.insert(didc);
        }
        // 3. 有，则更新
        else if (Objects.nonNull(didc.getExportFileId()) && !didc.getExportFileId().equals(exportFileId)){
            didc.setExportFileId(exportFileId);
            didc.setUpdateTime(LocalDateTime.now());
            designItemDetailCanvasMapper.updateById(didc);
        }
    }

    @Override
    public DesignItemDetailCanvas getDIDCByDesignItemDetailId(Long designItemDetailId){
        QueryWrapper<DesignItemDetailCanvas> qw = new QueryWrapper<>();
        qw.lambda().eq(DesignItemDetailCanvas::getDesignItemDetailId, designItemDetailId);

        return designItemDetailCanvasMapper.selectOne(qw);
    }

    public void updateDetailPathById(Long id, String path) {
        if (Objects.nonNull(id)) {
            DesignItemDetail designItemDetail = baseMapper.selectById(id);
            if (Objects.nonNull(designItemDetail)) {
                designItemDetail.setPath(path);
                designItemDetail.setUpdateDate(new Date());

                baseMapper.updateById(designItemDetail);
            }
        }
    }
}
