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.Partner;
import com.winsun.mapper.PartnerMapper;
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.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: chancy
 * @Date: 2020/2/12 17:26
 */
@Slf4j
@RestController
@RequestMapping("/school/partner")
public class PartnerController extends BaseController {

    private static PartnerMapper partnerMapper;


    @Autowired
    public PartnerController(PartnerMapper partnerMapper) {
        PartnerController.partnerMapper = partnerMapper;
    }

    @Permission(menuname = "修改合伙人信息", value = "update", method = RequestMethod.POST)
    public ResponseData<String> updatePartner(@RequestParam("id") String id,@RequestParam("phoneNum") String phoneNum, @RequestParam("school") String school
            , @RequestParam("name") String name, @RequestParam("account") String account, @RequestParam("idCard") String idCard, @RequestParam("ysm") String ysm
            , @RequestParam("gender") String gender, @RequestParam("area") String area, @RequestParam("wxId") String wxId) {
        int updateSysUse = partnerMapper.updateSysUser(phoneNum, name, account, gender, area, id);
        int updateHhUser = partnerMapper.updateHhUser(ysm,wxId,idCard,school,id);
        if (updateSysUse==1&&updateHhUser==1){
            return ResponseData.success("修改完毕！");
        }
        return ResponseData.error("修改失败");
    }

    @Permission(menuname = "查询合伙人信息", value = "list", method = RequestMethod.POST)
    public ResponseData<Page<Partner>> listPartner(@RequestParam("school") String school, @RequestParam("name") String name, @RequestParam("area") String area,
                                                         @RequestParam(name = "pageNo", required = false) int pageIndex, @RequestParam(name = "pageSize", required = false) int pageSize) {
        Wrapper<Partner> wrapper = new EntityWrapper();
        wrapper.like(StringUtils.isNotBlank(school), "school", school, SqlLike.DEFAULT);
        wrapper.like(StringUtils.isNotBlank(name), "name", name, SqlLike.DEFAULT);
        wrapper.eq(StringUtils.isNotBlank(area), "area", area);
        Page<Partner> page = new Page<>(pageIndex, pageSize);
        List<Partner> salespeople = partnerMapper.selectPage(page, wrapper);
        page.setRecords(salespeople);
        return ResponseData.success(page, "查询完毕！");
    }
}
