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.UniversityInfo;
import com.winsun.mapper.ProductMapper;
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.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

    private static UniversityInfoMapper universityInfoMapper;

    @Autowired
    public UniversityInfoController(UniversityInfoMapper universityInfoMapper) {
        UniversityInfoController.universityInfoMapper = universityInfoMapper;
    }

    /**
     * 添加大学宽带信息
     * @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, "查询成功！");
    }
}
