package com.cool.store.dao;

import com.cool.store.constants.CommonConstants;
import com.cool.store.entity.HyPartnerUserInfoDO;
import com.cool.store.entity.ShopAccountDO;
import com.cool.store.enums.OpenStatusEnum;
import com.cool.store.enums.PlatformBuildEnum;
import com.cool.store.enums.ShopAccountEnum;
import com.cool.store.mapper.ShopAccountMapper;
import com.cool.store.utils.StringUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author suzhuhong
 * @Date 2025/4/7 14:26
 * @Version 1.0
 */
@Component
public class ShopAccountDAO {

    @Resource
    ShopAccountMapper shopAccountMapper;

    public Integer initShopAccount(HyPartnerUserInfoDO partnerUserInfoDO, List<Long> shopIds){
        List<ShopAccountDO> shopAccountDOS = new ArrayList<>();
        for (Long shopId : shopIds) {
            for (ShopAccountEnum shopAccountEnum: ShopAccountEnum.values()) {
                ShopAccountDO shopAccountDO = new ShopAccountDO();
                shopAccountDO.setShopId(shopId);
                shopAccountDO.setSystemName(shopAccountEnum.getSystemName());
                shopAccountDO.setBoundPhone(partnerUserInfoDO.getMobile());
                //其他账户不使用统一密码
                if (ShopAccountEnum.getSpecialEnumList().contains(shopAccountEnum)){
                    shopAccountDO.setPasswordSalt(partnerUserInfoDO.getDownstreamSystemSalting());
                    shopAccountDO.setPassword(partnerUserInfoDO.getDownstreamSystemPassword());
                    shopAccountDO.setSecondaryPassword(partnerUserInfoDO.getDownstreamSystemSecondaryPassword());
                }
                shopAccountDO.setStatus(shopAccountEnum.getInitStatus().getCode());
                if (ShopAccountEnum.HUOMA.equals(shopAccountEnum)){
                    shopAccountDO.setEntryStatus(CommonConstants.ONE);
                }
                shopAccountDOS.add(shopAccountDO);
            }
        }
        return shopAccountMapper.batchInsert(shopAccountDOS);
    }


   public int batchInsert(List<ShopAccountDO> list){
        if (CollectionUtils.isEmpty(list)){
            return CommonConstants.ZERO;
        }
        return shopAccountMapper.batchInsert(list);
    }

    /**
     * 根据shopId查询多条数据
     * @param shopId 店铺ID
     * @return 店铺账号列表
     */
    public List<ShopAccountDO> selectByShopId(Long shopId){
        return shopAccountMapper.selectByShopId(shopId);
    }

    public ShopAccountDO selectByShopIdAndSystemName(Long shopId,String systemName){
        if (Objects.isNull(shopId)|| StringUtil.isEmpty(systemName)){
            return null;
        }

        return shopAccountMapper.selectByShopIdAndSystemName(shopId,systemName);
    }

    /**
     * 根据shopId与system_name修改status
     * @param shopId 店铺ID
     * @param systemNameList 系统名称
     * @param status 状态
     * @return 影响的行数
     */
    public int updateStatusByShopIdAndSystemName(Long shopId, List<String> systemNameList, Integer status,Date syncTime,String ylsCode){
        return shopAccountMapper.updateStatusByShopIdAndSystemName(shopId,systemNameList,status,syncTime,ylsCode);
    }

    public int updateEntryStatusByShopIdAndSystemName(Long shopId, List<String> systemNameList, Integer status){
        return shopAccountMapper.updateEntryStatusByShopIdAndSystemName(shopId,systemNameList,status);
    }

    /**
     * changeStatus
     * @param shopId
     * @param type
     * @param account
     * @param password
     * @param resultType
     * @return
     */
    public Boolean changeStatus(Long shopId,Integer type,String account ,String password,Integer resultType){
        if (Objects.isNull(shopId)||Objects.isNull(resultType)){
            return Boolean.FALSE;
        }
        Integer openStatus = resultType == 1 ? OpenStatusEnum.OPENSTATUSENUM_6.getCode() : OpenStatusEnum.OPENSTATUSENUM_5.getCode();
        String systemName = PlatformBuildEnum.getMessageByCode(type);
        shopAccountMapper.updateAccountByShopIdAndSystemName(shopId,systemName,account,password,openStatus);
        return Boolean.TRUE;

    }


    /**
     * 只修改 POS 云流水 新掌柜
     * @param shopIds
     * @param password
     * @param passwordSalt
     * @param lastSyncTime
     * @return
     */
    public int batchUpdatePasswordByShopIds( List<Long> shopIds, String password,String secondaryPassword, String passwordSalt, Date lastSyncTime){
        return shopAccountMapper.batchUpdatePasswordByShopIds(
                shopIds,  password, secondaryPassword, passwordSalt,  lastSyncTime
        );
    }


    public List<ShopAccountDO> getALlFail(){
        return shopAccountMapper.getALlFail();
    }

    public List<ShopAccountDO> getByShopIds(List<Long> shopIds){
        Example example = new Example(ShopAccountDO.class);
        example.createCriteria().andIn("shopId", shopIds);
        return shopAccountMapper.selectByExample(example);
    }

    public List<ShopAccountDO> getPasswordIsNullByShopIds(List<Long> shopIds){
        if (CollectionUtils.isEmpty(shopIds)){
            return new ArrayList<>();
        }
        Example example = new Example(ShopAccountDO.class);
        List<String> systemNameList = new ArrayList<>();
        systemNameList.add("火码POS");
        systemNameList.add("云流水");
        systemNameList.add("新掌柜");
        example.createCriteria().andIn("shopId", shopIds).andIsNull("password").andIn("systemName",systemNameList);
        return shopAccountMapper.selectByExample(example);
    }


    /**
     * @Auther: wangshuo
     * @Date: 2025/4/28
     * @description: 数据处理专用只处理，盐值 密码 第二密码
     */
    public Integer dateHandle(List<ShopAccountDO> list){
        if (CollectionUtils.isEmpty(list)){
            return CommonConstants.ZERO;
        }
        return shopAccountMapper.dateHandle(list);
    }
    /**
     * @Auther: wangshuo
     * @Date: 2025/4/28
     * @description: 获取特定字段 导出用
     */
    public List<ShopAccountDO> getSpecificByShopIds(List<Long> shopIds){
        return shopAccountMapper.getSpecificByShopIds(shopIds);
    }

    //数据处理用
    public List<Long> getPasswordIsNull(){
        return shopAccountMapper.getPasswordIsNull();
    }

    //数据处理用平台账号 火马，云流水，新掌柜状态改为已完成
    public Integer updateStatusDataHandle(List<Long> shopIds){
        if (CollectionUtils.isEmpty(shopIds)){
            return CommonConstants.ZERO;
        }
        return shopAccountMapper.updateStatusDataHandle(shopIds);
    }
    public List<Long> getShopId(){
        List<ShopAccountDO> shopAccountDOS = shopAccountMapper.selectAll();
        return new ArrayList<>(shopAccountDOS.stream().map(ShopAccountDO::getShopId).collect(Collectors.toSet())) ;
    }
}
