package com.winsun.controller;

import com.alibaba.fastjson.JSON;
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.auth.core.shiro.ShiroUser;
import com.winsun.bean.Product;
import com.winsun.bean.School;
import com.winsun.bean.UniversityInfo;
import com.winsun.mapper.ProductMapper;
import com.winsun.mapper.SchoolMapper;
import com.winsun.mapper.UniversityInfoMapper;
import com.winsun.utils.MyBatisPlusUpdateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.*;

/**
 * 大学宽带管理
 * @Author: chancy
 * @Date: 2020/2/12 17:26
 */
@Slf4j
@RestController
@RequestMapping("/university")
public class UniversityInfoController extends BaseController {

    private static UniversityInfoMapper universityInfoMapper;

    private static ProductMapper productMapper;

    private static SchoolMapper schoolMapper;

    @Autowired
    public UniversityInfoController(UniversityInfoMapper universityInfoMapper, ProductMapper productMapper,
                                    SchoolMapper schoolMapper) {
        UniversityInfoController.universityInfoMapper = universityInfoMapper;
        UniversityInfoController.productMapper = productMapper;
        UniversityInfoController.schoolMapper = schoolMapper;
    }

    /**
     * 添加大学宽带信息
     * @param university 添加对象的json字符串
     * @return
     */
    @Permission(menuname = "添加大学宽带信息", value = "insert", method = RequestMethod.POST)
    public ResponseData<String> insertUniversityInfo(@RequestParam("university") String university) {
        ShiroUser user = getShiroUser();
        if (!user.getRoleNames().stream().anyMatch(roleName -> StringUtils.equalsAny(roleName, "超级管理员"))) {
            return ResponseData.error("无数据权限");
        }
        UniversityInfo uni = JSON.parseObject(university, UniversityInfo.class);
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        uni.setUniversityCreateTime(timestamp);
        uni.setUniversityLastUpdateTime(timestamp);
        uni.setUniversityCreater(user.getName());
        universityInfoMapper.insert(uni);
        return ResponseData.success("添加成功！");
    }

    /**
     * 删除大学宽带信息
     * @param id  需要删除的id
     * @return
     */
    @Permission(menuname = "删除大学宽带信息", value = "delete/{id}", method = RequestMethod.POST)
    public ResponseData<String> deleteUniversityInfo(@PathVariable("id") Integer id) {
        ShiroUser user = getShiroUser();
        if (!user.getRoleNames().stream().anyMatch(roleName -> StringUtils.equalsAny(roleName, "超级管理员"))) {
            return ResponseData.error("无数据权限");
        }
        universityInfoMapper.deleteById(id);
        return ResponseData.success("删除成功！");
    }

    /**
     * 编辑大学宽带信息
     * @param university 编辑对象的json字符串
     * @return
     */
    @Permission(menuname = "修改宽带信息", value = "update", method = RequestMethod.POST)
    public ResponseData<String> updateUniversityInfo(@RequestParam("university") String university) {
        ShiroUser user = getShiroUser();
        if (!user.getRoleNames().stream().anyMatch(roleName -> StringUtils.equalsAny(roleName, "超级管理员"))) {
            return ResponseData.error("无数据权限");
        }
        UniversityInfo uni = JSON.parseObject(university, UniversityInfo.class);
        Wrapper wrapper = new EntityWrapper<>();
        wrapper.eq("university_id", uni.getUniversityId());
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        Map<String, Object> dataMapping = new HashMap<>();
        dataMapping.put("university_last_update_time", timestamp);
        dataMapping.put("university_name", uni.getUniversityName());
        dataMapping.put("university_region", uni.getUniversityRegion());
        dataMapping.put("university_installed_address", uni.getUniversityInstalledAddress());
        dataMapping.put("university_exterior_lines", uni.getUniversityExteriorLines());
        dataMapping.put("university_package_number", uni.getUniversityPackageNumber());
        dataMapping.put("university_user_type", uni.getUniversityUserType());
        dataMapping.put("university_contact_phone", uni.getUniversityContactPhone());
        dataMapping.put("university_monthly_rent_type", uni.getUniversityMonthlyRentType());
        dataMapping.put("university_charge_mode", uni.getUniversityChargeMode());
        dataMapping.put("university_charging_attribute", uni.getUniversityChargingAttribute());
        dataMapping.put("speed", uni.getSpeed());
        dataMapping.put("public_number", uni.getPublicNumber());
        universityInfoMapper.updateForSet(MyBatisPlusUpdateUtils.toUpdateSet(dataMapping), wrapper);
        return ResponseData.success("修改成功！");
    }

    /**
     *
     * @param universityName  学校名称
     * @param universityRegion 所在区域
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @Permission(menuname = "查询宽带信息", value = "list", method = RequestMethod.POST)
    public ResponseData<Page<UniversityInfo>> listUniversityInfo(@RequestParam("universityName") String universityName, @RequestParam("universityRegion") String universityRegion,
                                                         @RequestParam(name = "pageNo") int pageIndex, @RequestParam(name = "pageSize") int pageSize) {
        ShiroUser user = getShiroUser();
        if (!user.getRoleNames().stream().anyMatch(roleName -> StringUtils.equalsAny(roleName, "超级管理员"))) {
            return ResponseData.error("无数据权限");
        }
        Wrapper<UniversityInfo> wrapper = new EntityWrapper();
        wrapper.like(StringUtils.isNotBlank(universityName), "university_name", universityName, SqlLike.DEFAULT);
        wrapper.like(StringUtils.isNotBlank(universityRegion), "university_region", universityRegion, SqlLike.DEFAULT);
        Page<UniversityInfo> page = new Page<>(pageIndex, pageSize);
        List<UniversityInfo> universitys = universityInfoMapper.selectPage(page, wrapper);
        page.setRecords(universitys);
        return ResponseData.success(page, "查询成功！");
    }

    /**
     *
     * @param productUniversity  学校名称
     * @param productRegion 所在区域
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @Permission(menuname = "查询宽带配置信息列表", value = "broadbandConfigList", method = RequestMethod.POST)
    public ResponseData<Page<Map<String, Object>>> broadbandConfigList(@RequestParam(value = "productUniversity", required = false) String productUniversity,
                                                                   @RequestParam(value = "productRegion", required = false) String productRegion,
                                                                 @RequestParam(name = "pageNo") int pageIndex, @RequestParam(name = "pageSize") int pageSize) {
        ShiroUser user = getShiroUser();
        if (!user.getRoleNames().stream().anyMatch(roleName -> StringUtils.equalsAny(roleName, "超级管理员"))) {
            return ResponseData.error("无数据权限");
        }

        Page<Map<String, Object>> page = new Page<>(pageIndex, pageSize);
        List<Map<String, Object>> broadBandConfigList = universityInfoMapper.getBroadBandConfigList(page, productUniversity, productRegion);
        page.setRecords(broadBandConfigList);
        return ResponseData.success(page, "查询成功！");
    }

    /**
     * 查询宽带编辑信息
     * @param universityId
     * @return
     */
    @Permission(menuname = "查询宽带配置信息", value="broadbandConfigData", method=RequestMethod.POST)
    public ResponseData<Map<String, Object>> broadbandConfigData(@RequestParam("universityId") Integer universityId) {

        Map<String, Object> result = new HashMap<>();
        // 查询大学宽带配置
        UniversityInfo universityInfo = universityInfoMapper.selectById(universityId);
        if (universityInfo != null) {
            result.put("universityInfoData", universityInfo);
            // 查询产品配置
            Wrapper<Product> productWrapper = new EntityWrapper<>();
            productWrapper.eq("university_id", universityInfo.getUniversityId());
            List<Product> products = productMapper.selectList(productWrapper);
            if (products != null && products.size() != 0) {
                result.put("productData", products.get(0));
                // 查询学校配置
                Wrapper<School> schoolWrapper = new EntityWrapper<>();
                schoolWrapper.eq("bandproduct_id", products.get(0).getProductId());
                List<School> schools = schoolMapper.selectList(schoolWrapper);
                result.put("schoolsData", schools);
            }
        }else{
            return ResponseData.error("未查询到当前宽带信息");
        }
        return ResponseData.success(result);
    }

    /**
     * 宽带适用学校
     * @return
     */
    @Permission(menuname = "单宽学校名称", value = "universitySchool", method = RequestMethod.POST)
    public ResponseData< List<UniversityInfo>> universitySchool(){
        Wrapper<UniversityInfo> wrapper = new EntityWrapper();
        List<UniversityInfo> universityInfos = universityInfoMapper.selectList(wrapper);
        return ResponseData.success(universityInfos);
    }

}
