package com.yiqi.app.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yiqi.app.domain.AppCouponTypeApplyOrg;
import com.yiqi.app.domain.dto.AppCouponTypeDTO;
import com.yiqi.app.domain.vo.AppCouponTypeVO;
import com.yiqi.app.service.IAppCouponTypeApplyOrgService;
import com.yiqi.common.core.domain.vo.SelectItemVO;
import com.yiqi.common.enums.DeleteStatus;
import com.yiqi.common.enums.SourceType;
import com.yiqi.common.enums.StatusType;
import com.yiqi.common.utils.DateUtils;
import com.yiqi.common.utils.SecurityUtils;
import com.yiqi.system.domain.ManageFactory;
import com.yiqi.system.domain.SysStore;
import com.yiqi.system.service.IManageFactoryService;
import com.yiqi.system.service.ISysStoreService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yiqi.app.mapper.AppCouponTypeMapper;
import com.yiqi.app.domain.AppCouponType;
import com.yiqi.app.service.IAppCouponTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 优惠券类型Service业务层处理
 *
 * @author xiaohei
 * @date 2023-09-06
 */
@Service
public class AppCouponTypeServiceImpl extends ServiceImpl<AppCouponTypeMapper,AppCouponType> implements IAppCouponTypeService {

    @Autowired
    private IAppCouponTypeApplyOrgService appCouponApplyOrgService;
    @Autowired
    private ISysStoreService sysStoreService;
    @Autowired
    private IManageFactoryService manageFactoryService;


    /**
     * 查询优惠券类型
     *
     * @param id 优惠券类型主键
     * @return 优惠券类型
     */
    @Override
    public AppCouponTypeVO selectAppCouponTypeById(Long id) {
        AppCouponTypeVO appCouponTypeVO = new AppCouponTypeVO();
        appCouponTypeVO.setStoreVOS(sysStoreService.list(
                        new QueryWrapper<SysStore>().lambda()
                                .eq(SysStore::getDelFlag, DeleteStatus.OK.getCode())
                                .eq(SysStore::getStatus, StatusType.OK.getCode())).stream()
                .map(SelectItemVO::new).collect(Collectors.toList()));
//        appCouponTypeVO.setFactoryVOS(manageFactoryService.list(
//                        new QueryWrapper<ManageFactory>().lambda()
//                                .eq(ManageFactory::getDelFlag, DeleteStatus.OK.getCode())
//                                .eq(ManageFactory::getStatus, StatusType.OK.getCode())).stream()
//                .map(SelectItemVO::new).collect(Collectors.toList()));
        if(id != null){
            AppCouponType appCouponType = baseMapper.selectAppCouponTypeById(id);
            BeanUtils.copyProperties(appCouponType,appCouponTypeVO);
            List<AppCouponTypeApplyOrg> appCouponTypeApplyOrgs = appCouponApplyOrgService.selectAppCouponTypeApplyOrgList(new AppCouponTypeApplyOrg(id));
            appCouponTypeVO.setSelectStoreIds(appCouponTypeApplyOrgs.stream().filter(item -> SourceType.STORE.getCode().equals(item.getSourceType())).map(AppCouponTypeApplyOrg::getOrgId).collect(Collectors.toList()));
            appCouponTypeVO.setSelectFactoryIds(appCouponTypeApplyOrgs.stream().filter(item -> SourceType.FACTORY.getCode().equals(item.getSourceType())).map(AppCouponTypeApplyOrg::getOrgId).collect(Collectors.toList()));
        }
        return appCouponTypeVO;
    }

    /**
     * 查询优惠券类型列表
     *
     * @param appCouponType 优惠券类型
     * @return 优惠券类型
     */
    @Override
    public List<AppCouponType> selectAppCouponTypeList(AppCouponType appCouponType) {
        return baseMapper.selectAppCouponTypeList(appCouponType);
    }

    /**
     * 新增优惠券类型
     *
     * @param appCouponTypeDTO 优惠券类型
     * @return 结果
     */
    @Override
    public int insertAppCouponType(AppCouponTypeDTO appCouponTypeDTO) {
        AppCouponType appCouponType = new AppCouponType();
        BeanUtils.copyProperties(appCouponTypeDTO,appCouponType);
        appCouponType.setCreateById(SecurityUtils.getUserId());
        appCouponType.setCreateBy(SecurityUtils.getUsername());
        appCouponType.setCreateTime(DateUtils.getNowDate());
        int rows = baseMapper.insert(appCouponType);
        List<AppCouponTypeApplyOrg> appCouponTypeApplyOrgs = new ArrayList<>();
        for (Long selectStoreId : appCouponTypeDTO.getSelectStoreIds()) {
            AppCouponTypeApplyOrg appCouponTypeApplyOrg = new AppCouponTypeApplyOrg();
            appCouponTypeApplyOrg.setCouponTypeId(appCouponType.getId());
            appCouponTypeApplyOrg.setSourceType(SourceType.STORE.getCode());
            appCouponTypeApplyOrg.setOrgId(selectStoreId);
            appCouponTypeApplyOrg.setCreateTime(appCouponType.getCreateTime());
            appCouponTypeApplyOrg.setCreateBy(appCouponType.getCreateBy());
            appCouponTypeApplyOrg.setCreateById(appCouponType.getCreateById());
            appCouponTypeApplyOrgs.add(appCouponTypeApplyOrg);
        }
        for (Long selectFactoryId : appCouponTypeDTO.getSelectFactoryIds()) {
            AppCouponTypeApplyOrg appCouponTypeApplyOrg = new AppCouponTypeApplyOrg();
            appCouponTypeApplyOrg.setCouponTypeId(appCouponType.getId());
            appCouponTypeApplyOrg.setSourceType(SourceType.FACTORY.getCode());
            appCouponTypeApplyOrg.setOrgId(selectFactoryId);
            appCouponTypeApplyOrg.setCreateTime(appCouponType.getCreateTime());
            appCouponTypeApplyOrg.setCreateBy(appCouponType.getCreateBy());
            appCouponTypeApplyOrg.setCreateById(appCouponType.getCreateById());
            appCouponTypeApplyOrgs.add(appCouponTypeApplyOrg);
        }
        appCouponApplyOrgService.saveBatch(appCouponTypeApplyOrgs);
        return rows;
    }

    /**
     * 修改优惠券类型
     *
     * @param appCouponTypeDTO 优惠券类型
     * @return 结果
     */
    @Override
    public int updateAppCouponType(AppCouponTypeDTO appCouponTypeDTO) {
        AppCouponType appCouponType = new AppCouponType();
        BeanUtils.copyProperties(appCouponTypeDTO,appCouponType);
        appCouponType.setUpdateTime(DateUtils.getNowDate());
        appCouponType.setUpdateById(SecurityUtils.getUserId());
        appCouponType.setUpdateBy(SecurityUtils.getUsername());
        appCouponApplyOrgService.remove(new QueryWrapper<AppCouponTypeApplyOrg>().lambda()
                .eq(AppCouponTypeApplyOrg::getCouponTypeId,appCouponTypeDTO.getId()));

        int rows = baseMapper.updateById(appCouponType);

        List<AppCouponTypeApplyOrg> appCouponTypeApplyOrgs = new ArrayList<>();
        for (Long selectStoreId : appCouponTypeDTO.getSelectStoreIds()) {
            AppCouponTypeApplyOrg appCouponTypeApplyOrg = new AppCouponTypeApplyOrg();
            appCouponTypeApplyOrg.setCouponTypeId(appCouponType.getId());
            appCouponTypeApplyOrg.setSourceType(SourceType.STORE.getCode());
            appCouponTypeApplyOrg.setOrgId(selectStoreId);
            appCouponTypeApplyOrg.setCreateTime(appCouponType.getCreateTime());
            appCouponTypeApplyOrg.setCreateBy(appCouponType.getCreateBy());
            appCouponTypeApplyOrg.setCreateById(appCouponType.getCreateById());
            appCouponTypeApplyOrgs.add(appCouponTypeApplyOrg);
        }
        for (Long selectFactoryId : appCouponTypeDTO.getSelectFactoryIds()) {
            AppCouponTypeApplyOrg appCouponTypeApplyOrg = new AppCouponTypeApplyOrg();
            appCouponTypeApplyOrg.setCouponTypeId(appCouponType.getId());
            appCouponTypeApplyOrg.setSourceType(SourceType.FACTORY.getCode());
            appCouponTypeApplyOrg.setOrgId(selectFactoryId);
            appCouponTypeApplyOrg.setCreateTime(appCouponType.getCreateTime());
            appCouponTypeApplyOrg.setCreateBy(appCouponType.getCreateBy());
            appCouponTypeApplyOrg.setCreateById(appCouponType.getCreateById());
            appCouponTypeApplyOrgs.add(appCouponTypeApplyOrg);
        }
        appCouponApplyOrgService.saveBatch(appCouponTypeApplyOrgs);
        return rows;
    }

    /**
     * 批量删除优惠券类型
     *
     * @param ids 需要删除的优惠券类型主键
     * @return 结果
     */
    @Override
    public int deleteAppCouponTypeByIds(Long[] ids) {
        return baseMapper.deleteAppCouponTypeByIds(ids);
    }

    /**
     * 删除优惠券类型信息
     *
     * @param id 优惠券类型主键
     * @return 结果
     */
    @Override
    public int deleteAppCouponTypeById(Long id) {
        return baseMapper.deleteAppCouponTypeById(id);
    }

    /**
     * 修改优惠券类型状态
     *
     * @param appCouponType 优惠券类型
     * @return 结果
     */
    @Override
    public int updateAppCouponTypeStatus(AppCouponType appCouponType) {
        appCouponType.setUpdateById(SecurityUtils.getUserId());
        appCouponType.setUpdateBy(SecurityUtils.getUsername());
        appCouponType.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateAppCouponType(appCouponType);
    }


}
