package com.ruoyi.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.demo.entity.*;
import com.ruoyi.demo.entity.bo.CallBackBean;
import com.ruoyi.demo.entity.vo.AppVo;
import com.ruoyi.demo.mapper.*;
import com.ruoyi.demo.service.AppService;
import com.ruoyi.demo.utils.SendCallBackUtil;
import com.ruoyi.demo.utils.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.List;

@Service
@Transactional
public class AppServiceImpl implements AppService {
    @Autowired
    AppMapper appMapper;

    @Autowired
    CommercialMapper commercialMapper;

    @Autowired
    PlanMapper planMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    OrderMapper orderMapper;

    @Override
    public R addApp(AppVo appVo) {
        //判断是否存在该商户
        QueryWrapper<Commercial> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("commercial_id",appVo.getCommercialId());
        Long aLong = commercialMapper.selectCount(queryWrapper);
        if (aLong > 0){
            App app = new App(appVo);
            //生成appkey
            app.setAppkey(UuidUtil.getUUID());
            //生成密钥
            app.setAppSecret(UuidUtil.getUUID());
            //时间
            app.setCreateTime(LocalDateTime.now(ZoneId.of("Asia/Shanghai")));
            appMapper.insert(app);
            return R.ok("添加成功");
        }
        return R.fail("不存在该商户");

    }

    @Override
    public void deleteApp(String[] appKeys) {
        appMapper.deleteBatchIds(Arrays.asList(appKeys));
    }

    @Override
    public PageInfo<App> getAppListByCommercialId(AppVo appVo) {
        QueryWrapper<App> queryWrapper = new QueryWrapper<>();
        if (appVo.getSearchText() != null && !appVo.getSearchText().equals("")){
            queryWrapper.likeRight("appkey",appVo.getSearchText());
            queryWrapper.or().like("app_name",appVo.getSearchText());
            queryWrapper.or().likeRight("commercial_name",appVo.getSearchText());
        }

        PageHelper.startPage(appVo.getPageNum(),appVo.getPageSize());
        List<App> apps = appMapper.selectList(queryWrapper);
        PageInfo<App> pageInfo = new PageInfo<>(apps);
        return pageInfo;
    }

    @Override
    public App getApp(AppVo appVo) {
        App app = appMapper.selectById(appVo.getAppkey());
        return app;
    }

    @Override
    public R updateApp(AppVo appVo) {
        //判断是否存在该商户
        QueryWrapper<App> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("app_name","state");
        queryWrapper.eq("appkey",appVo.getAppkey());
        App app = appMapper.selectOne(queryWrapper);
        if (app != null){
            //1.判断是否关闭小程序权限
            if((appVo.getState() != app.getState()) && appVo.getState() == 0 ){
                //禁用,并下架所有的广告
                UpdateWrapper<Plan> planUpdateWrapper = new UpdateWrapper<>();
                planUpdateWrapper.eq("appkey",appVo.getAppkey());
                planUpdateWrapper.set("on_state",0);
                if (!appVo.equals(app.getAppName()))
                    planUpdateWrapper.set("app_name",app.getAppName());
                planMapper.update(null,planUpdateWrapper);
            }

            //2.修改信息
            UpdateWrapper<App> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("appkey",appVo.getAppkey())
                .set("app_logo",appVo.getAppLogo())
                .set("app_name",appVo.getAppName())
                .set("app_introduce",appVo.getAppIntroduce())
                .set("commercial_id",appVo.getCommercialId())
                .set("commercial_name",appVo.getCommercialName())
                .set("state",appVo.getState())
                .set("call_back",appVo.getCallBack())
                .set("prove_1",appVo.getProve1())
                .set("prove_2",appVo.getProve2())
                .set("prove_3",appVo.getProve3())
                .set("prove_4",appVo.getProve4())
                .set("prove_5",appVo.getProve5())
                .set("annotation",appVo.getAnnotation());
            appMapper.update(null,updateWrapper);

            //3.修改冗余信息
            if (!appVo.equals(app.getAppName())){
                UpdateWrapper<Goods> updateWrapper1 = new UpdateWrapper<>();
                updateWrapper1.eq("appkey",appVo.getAppkey());
                updateWrapper1.set("app_name",appVo.getAppName());
                goodsMapper.update(null,updateWrapper1);

                UpdateWrapper<Order> updateWrapper3 = new UpdateWrapper<>();
                updateWrapper3.eq("appkey",appVo.getAppkey());
                updateWrapper3.set("app_name",appVo.getAppName());
                orderMapper.update(null,updateWrapper3);
            }

            return R.ok("修改成功");
        }else
            return R.fail("不存在该小程序");
    }

    @Override
    public void testCallBack(String callBack) {
        SendCallBackUtil.testCallBack(callBack);
    }

    @Override
    public Page<App> getAppList(AppVo appVo) {
        QueryWrapper<App> queryWrapper = new QueryWrapper<>();
        if (appVo.getSearchText() != null && !appVo.getSearchText().equals("")){
            queryWrapper.likeRight("appkey",appVo.getSearchText());
            queryWrapper.or().like("app_name",appVo.getSearchText());
            queryWrapper.or().likeRight("commercial_name",appVo.getSearchText());
        }

        Page<App> page = new Page<>();
        page.setCurrent(appVo.getPageNum());
        page.setSize(appVo.getPageSize());
        Page<App> appPage = appMapper.selectPage(page, queryWrapper);
        return appPage;
    }

    @Override
    public List<Commercial> getCommercialList() {
        QueryWrapper<Commercial> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("commercial_id","commercial_name");
        List<Commercial> commercials = commercialMapper.selectList(queryWrapper);
        return commercials;
    }
}
