package com.cool.store.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.cool.store.context.LoginUserInfo;
import com.cool.store.dao.MatterConfigDAO;
import com.cool.store.dto.notice.CommonDTO;
import com.cool.store.entity.MatterConfigDO;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.exception.ServiceException;
import com.cool.store.request.notice.MatterConfigAddRequest;
import com.cool.store.request.notice.MatterConfigQueryRequest;
import com.cool.store.service.MatterConfigService;
import com.cool.store.vo.notice.MatterAndMatterDetailVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

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

/**
 * @Author suzhuhong
 * @Date 2025/7/24 16:41
 * @Version 1.0
 */
@Service
public class MatterConfigServiceImpl implements MatterConfigService {

    @Resource
    MatterConfigDAO matterConfigDAO;

    @Override
    public int addMatterConfig(MatterConfigAddRequest request, LoginUserInfo currentUser) {
        MatterConfigDO matterConfigDO = new MatterConfigDO();
        BeanUtils.copyProperties(request, matterConfigDO);
        matterConfigDO.setCreateUserId(currentUser.getUserId());
        if (CollectionUtils.isNotEmpty(request.getStoreInfoList())){
            matterConfigDO.setDefaultStoreInfo(JSONObject.toJSONString(request.getStoreInfoList()));
        }
        if (CollectionUtils.isNotEmpty(request.getUserInfoList())){
            matterConfigDO.setDefaultHandlePersonInfo(JSONObject.toJSONString(request.getUserInfoList()));
        }
        return matterConfigDAO.insert(matterConfigDO);
    }

    @Override
    public int editMatterConfig(MatterConfigAddRequest request, LoginUserInfo currentUser) {
        if (request.getId() == null){
            throw new ServiceException(ErrorCodeEnum.PARAMS_VALIDATE_ERROR);
        }
        MatterConfigDO matterConfigDO = new MatterConfigDO();
        BeanUtils.copyProperties(request, matterConfigDO);
        matterConfigDO.setUpdateUserId(currentUser.getUserId());
        matterConfigDO.setUpdateTime(new Date());
        if (CollectionUtils.isNotEmpty(request.getStoreInfoList())){
            matterConfigDO.setDefaultStoreInfo(JSONObject.toJSONString(request.getStoreInfoList()));
        }
        if (CollectionUtils.isNotEmpty(request.getUserInfoList())){
            matterConfigDO.setDefaultHandlePersonInfo(JSONObject.toJSONString(request.getUserInfoList()));
        }
        return matterConfigDAO.updateForce(matterConfigDO);
    }

    @Override
    public Boolean deleteMatterConfig(List<Long> ids, LoginUserInfo currentUser) {
        matterConfigDAO.batchDelete(ids,currentUser.getUserId());
        return Boolean.TRUE;
    }

    @Override
    public PageInfo<MatterAndMatterDetailVO> listByCondition(MatterConfigQueryRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<MatterConfigDO> matterConfigDOS = matterConfigDAO.ListByCondition(request);

        if (CollectionUtils.isEmpty(matterConfigDOS)){
            return new PageInfo<>();
        }
        PageInfo result = new PageInfo<>(matterConfigDOS);
        List<MatterAndMatterDetailVO> list = new ArrayList<>();
        matterConfigDOS.forEach(x->{
            MatterAndMatterDetailVO matterAndMatterDetailVO = new MatterAndMatterDetailVO();
            BeanUtils.copyProperties(x,matterAndMatterDetailVO);
            matterAndMatterDetailVO.setStoreInfoList(JSONObject.parseArray(x.getDefaultStoreInfo(), CommonDTO.class));
            matterAndMatterDetailVO.setUserInfoList(JSONObject.parseArray(x.getDefaultHandlePersonInfo(),CommonDTO.class));
            list.add(matterAndMatterDetailVO);
        });
        result.setList(list);
        return result;
    }


}
