package com.winsun.controller;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.winsun.auth.core.annotion.Permission;
import com.winsun.auth.core.base.controller.BaseController;
import com.winsun.auth.core.common.model.ResponseData;
import com.winsun.bean.AuthCode;
import com.winsun.bean.KdyList;
import com.winsun.bean.MultimediaAccount;
import com.winsun.mapper.AuthCodeMapper;
import com.winsun.mapper.MultimediaAccountMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: chenhaojian
 * @Date: 2021/1/14
 */
@RestController
@RequestMapping("/multimediaAccount")
public class MultimediaAccountController extends BaseController {

    private static MultimediaAccountMapper multimediaAccountMapper;

    @Autowired
    public MultimediaAccountController(MultimediaAccountMapper multimediaAccountMapper){
        MultimediaAccountController.multimediaAccountMapper = multimediaAccountMapper;
    }


    @Permission(menuname = "查询信息", value = "list", method = RequestMethod.POST)
    public Map<String,Object> dataList(@RequestParam(name = "id",required = false)String id,
                                       @RequestParam(name = "accountNumber",required = false)String accountNumber,
                                       @RequestParam(name = "pageNo", required = false) int pageNo,
                                       @RequestParam(name = "pageSize", required = false) int pageSize){

        Map<String,Object> map = new HashMap<>();
        Wrapper<MultimediaAccount> wrapper = new EntityWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id",id);
        wrapper.like(StringUtils.isNotBlank(accountNumber), "account_number",accountNumber, SqlLike.RIGHT);

        Page<MultimediaAccount> page = new Page<>(pageNo, pageSize);
        List<MultimediaAccount> aPackage = multimediaAccountMapper.selectPage(page, wrapper);
        page.setRecords(aPackage);

        map.put("page",ResponseData.success(page,"查询成功！"));
        return map;
    }

    @RequestMapping(value = "addData",method = RequestMethod.POST)
    public ResponseData<String> addData(@RequestParam(name = "id") String id,
                                        @RequestParam(name = "accountNumber") String accountNumber){
        Wrapper<MultimediaAccount> wrapper = new EntityWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(accountNumber), "account_number",accountNumber);
        List<MultimediaAccount> list = multimediaAccountMapper.selectList(wrapper);
        if(list != null && list.size()>0){
            return ResponseData.error("多媒体账号已存在！");
        }
        MultimediaAccount multimediaAccount = new MultimediaAccount();
        multimediaAccount.setId(id);
        multimediaAccount.setAccountNumber(accountNumber);
        try {
            multimediaAccountMapper.insert(multimediaAccount);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseData.success();
    }

    @RequestMapping(value = "deleteData",method = RequestMethod.POST)
    public ResponseData<String> deleteData(@RequestParam(name = "id") String id){
        if(StringUtils.isBlank(id)){
            return ResponseData.error("");
        };
        Wrapper<MultimediaAccount> wrapper = new EntityWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id",id);
        try {
            multimediaAccountMapper.delete(wrapper);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseData.success();
    }
}
