package co.yixiang.yshop.module.shop.service.canvasdetail;

import cn.hutool.core.util.ObjectUtil;
import co.yixiang.yshop.module.shop.controller.admin.canvas.vo.CanvasDetailReqVO;
import co.yixiang.yshop.module.shop.controller.admin.canvas.vo.CanvasJsonUpdateReqVO;
import co.yixiang.yshop.module.shop.controller.admin.canvasdetail.vo.CanvasDetailRespVO;
import co.yixiang.yshop.module.shop.controller.admin.canvasdetail.vo.CanvasDetailUpdateReqVO;
import co.yixiang.yshop.module.shop.controller.app.canvas.vo.CanvasEnableReqVO;
import co.yixiang.yshop.module.shop.convert.canvasdetail.CanvasDetailConvert;
import co.yixiang.yshop.module.shop.dal.dataobject.canvas.CanvasDO;
import co.yixiang.yshop.module.shop.dal.dataobject.canvasdetail.CanvasDetailDO;
import co.yixiang.yshop.module.shop.dal.mysql.canvas.CanvasMapper;
import co.yixiang.yshop.module.shop.enums.canvas.CanvasTerminalEnum;
import co.yixiang.yshop.module.shop.enums.common.StateEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import co.yixiang.yshop.module.shop.dal.mysql.canvasdetail.CanvasDetailMapper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import static co.yixiang.yshop.framework.common.exception.util.ServiceExceptionUtil.exception;
import static co.yixiang.yshop.module.shop.enums.ErrorCodeConstants.CANVAS_DETAIL_NOT_EXISTS;

/**
 * 画布明细 Service 实现类
 *
 * @author moxiangrong
 */
@Service
@Validated
public class CanvasDetailServiceImpl implements CanvasDetailService {

    @Resource
    private CanvasDetailMapper canvasDetailMapper;

    @Resource
    private CanvasMapper canvasMapper;

    @Override
    public List<CanvasDetailRespVO> getListByCanvasId(Long canvasId) {
        return CanvasDetailConvert.INSTANCE.convertList(canvasDetailMapper
                .selectList(new LambdaQueryWrapper<CanvasDetailDO>().eq(CanvasDetailDO::getCanvasId, canvasId)));
    }

    @Override
    public CanvasDetailRespVO getCanvasDetail(CanvasDetailReqVO reqVO) {
        return CanvasDetailConvert.INSTANCE.convert(canvasDetailMapper
                .selectOne(new LambdaQueryWrapper<CanvasDetailDO>()
                        .eq(CanvasDetailDO::getCanvasId, reqVO.getId())
                        .eq(CanvasDetailDO::getTerminal, reqVO.getTerminal())
                        .last("limit 1")));
    }

    @Override
    public void batchInsert(Long canvasId) {
        List<CanvasDetailDO> insertDOS = new ArrayList<>();
        CanvasDetailDO mini = new CanvasDetailDO();
        mini.setCanvasId(canvasId);
        mini.setJson("");
        mini.setTerminal(CanvasTerminalEnum.MINI.getValue());
        insertDOS.add(mini);
        CanvasDetailDO h5 = new CanvasDetailDO();
        h5.setCanvasId(canvasId);
        h5.setJson("");
        h5.setTerminal(CanvasTerminalEnum.H5.getValue());
        insertDOS.add(h5);
        CanvasDetailDO app = new CanvasDetailDO();
        app.setCanvasId(canvasId);
        app.setJson("");
        app.setTerminal(CanvasTerminalEnum.APP.getValue());
        insertDOS.add(app);
        canvasDetailMapper.insertBatch(insertDOS);
    }

    @Override
    public void updateJson(CanvasJsonUpdateReqVO vo) {
        canvasDetailMapper.update(null, new LambdaUpdateWrapper<CanvasDetailDO>()
                .eq(CanvasDetailDO::getCanvasId, vo.getId())
                .eq(CanvasDetailDO::getTerminal, vo.getTerminal())
                .set(CanvasDetailDO::getJson, vo.getJson())
        );
    }

    @Override
    public void batchDelete(Long canvasId) {
        canvasDetailMapper.delete(new LambdaQueryWrapper<CanvasDetailDO>().eq(CanvasDetailDO::getCanvasId, canvasId));
    }

    @Override
    public CanvasDetailRespVO getCanvasJson(CanvasEnableReqVO reqVO) {
        CanvasDO canvasDO = canvasMapper.selectOne(new LambdaQueryWrapper<CanvasDO>()
                .eq(ObjectUtil.isNotNull(reqVO.getType()), CanvasDO::getType, reqVO.getType())
                .eq(CanvasDO::getState, StateEnum.OPEN.getValue()).last("limit 1")
        );
        if (ObjectUtil.isNull(canvasDO)) {
            return null;
        }
        CanvasDetailDO canvasDetailDO = canvasDetailMapper.selectOne(new LambdaQueryWrapper<CanvasDetailDO>()
                .eq(CanvasDetailDO::getCanvasId, canvasDO.getId())
                .eq(CanvasDetailDO::getTerminal, reqVO.getTerminal()).last("limit 1")
        );
        return CanvasDetailConvert.INSTANCE.convert(canvasDetailDO);
    }
}
