package com.winsun.controller;

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.Package;
import com.winsun.bean.PackageUpgrade;
import com.winsun.mapper.PackageMapper;
import com.winsun.mapper.PackageUpgradeMapper;
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.*;

/**
 *
 * @Date: 2021/6/29
 */
@RestController
@RequestMapping("/packageUpgrade")
public class PackageUpgradeController extends BaseController {

    @Autowired
    private PackageUpgradeMapper packageUpgradeMapper;

    @Autowired
    private PackageMapper packageMapper;



    @Permission(menuname = "查询信息", value = "list", method = RequestMethod.POST)
    public Map<String,Object> dataList(@RequestParam(name = "accNbr",required = false)String accNbr,
                                       @RequestParam(name = "cdmaDiscDesc",required = false)String cdmaDiscDesc,
                                       @RequestParam(name = "wxcl",required = false)String wxcl,
                                       @RequestParam(name = "pageNo", required = false) int pageNo,
                                       @RequestParam(name = "pageSize", required = false) int pageSize
    ){
        Map<String,Object> map = new HashMap<>();
        try {
            String package1 = "";
            String package2 = "";
            if (StringUtils.isNotBlank(cdmaDiscDesc)){
                package1 = packageMapper.selectById(cdmaDiscDesc).getPackageName();
            }
            if (StringUtils.isNotBlank(wxcl)){
                package2 = packageMapper.selectById(wxcl).getPackageName();
            }
            Wrapper<PackageUpgrade> wrapper = new EntityWrapper<>();
            wrapper.setSqlSelect("serv_id as servId,acc_nbr as accNbr,cdma_disc_desc as cdmaDiscDesc,wxcl");
            wrapper.eq(StringUtils.isNotBlank(accNbr), "acc_nbr",accNbr);
            wrapper.eq(StringUtils.isNotBlank(package1), "cdma_disc_desc",package1);
            wrapper.eq(StringUtils.isNotBlank(package2), "wxcl",package2);
            com.baomidou.mybatisplus.plugins.Page<PackageUpgrade> page = new Page<>(pageNo, pageSize);
            List<PackageUpgrade> aPackage = packageUpgradeMapper.selectPage(page, wrapper);
            page.setRecords(aPackage);
            map.put("page",ResponseData.success(page,"查询成功！"));
        }catch (Exception e){
            e.printStackTrace();
        }

        return map;
    }

    @RequestMapping(value = "addData",method = RequestMethod.POST)
    public ResponseData<String> addData(@RequestParam(name = "accNbr") String accNbr,
                                        @RequestParam(name = "cdmaDiscDesc") String cdmaDiscDesc,
                                        @RequestParam(name = "wxcl") String wxcl){


        Wrapper<PackageUpgrade> wrapper = new EntityWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(accNbr), "acc_nbr",accNbr);
        packageUpgradeMapper.delete(wrapper);
        PackageUpgrade packageUpgrade = new PackageUpgrade();
        packageUpgrade.setServId(Double.valueOf(accNbr));
        packageUpgrade.setAccNbr(accNbr);
        Date date = new Date();
        packageUpgrade.setCustName("测试");
        packageUpgrade.setCreateDateKd(date);
        packageUpgrade.setFinishDate(date);
        packageUpgrade.setStopDateKd(date);
        try {
            String package1 = "";
            String package2 = "";
            if (StringUtils.isNotBlank(cdmaDiscDesc)){
                package1 = packageMapper.selectById(cdmaDiscDesc).getPackageName();
            }
            if (StringUtils.isNotBlank(wxcl)){
                package2 = packageMapper.selectById(wxcl).getPackageName();
            }
            packageUpgrade.setCdmaDiscDesc(package1);
            packageUpgrade.setWxcl(package2);
            packageUpgradeMapper.insert(packageUpgrade);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseData.success();
    }

    @RequestMapping(value = "deleteData",method = RequestMethod.POST)
    public ResponseData<String> deleteData(@RequestParam(name = "accNbr") String accNbr){
        if(StringUtils.isBlank(accNbr)){
            return ResponseData.error("");
        };
        Wrapper<PackageUpgrade> wrapper = new EntityWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(accNbr), "acc_nbr",accNbr);
        try {
            packageUpgradeMapper.delete(wrapper);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseData.success();
    }
    @RequestMapping(value = "getPackage",method = RequestMethod.POST)
    public ResponseData<List<Map<String,Object>>> getPackage(){
        Wrapper<Package> wrapper = new EntityWrapper<>();
        wrapper.setSqlSelect("id,name,package_name as packageName");
        List<Map<String,Object>> packageList = packageMapper.selectMaps(wrapper);
        return ResponseData.success(packageList);
    }
}
