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.common.model.ResponseData;
import com.winsun.bean.HhrUser;
import com.winsun.bean.SalesOrder;
import com.winsun.bean.SysUser;
import com.winsun.mapper.HhrUserMapper;
import com.winsun.mapper.SalesOrderMapper;
import com.winsun.mapper.SysUserMapper;
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.ArrayList;
import java.util.List;

/**
 * @Author: hsl
 * @Date: 2020/5/13 15:41
 */
@RestController
@Slf4j
@RequestMapping("salesOrder")
public class HhrSalesOrderController {

    private static SalesOrderMapper salesOrderMapper;
    
    private static SysUserMapper sysUserMapper;

    private static HhrUserMapper hhrUserMapper;

    @Autowired
    public HhrSalesOrderController(SalesOrderMapper salesOrderMapper,SysUserMapper sysUserMapper,HhrUserMapper hhrUserMapper){
        HhrSalesOrderController.salesOrderMapper=salesOrderMapper;
        HhrSalesOrderController.sysUserMapper=sysUserMapper;
        HhrSalesOrderController.hhrUserMapper=hhrUserMapper;
    }

    @Permission(menuname = "学子公司销量",value = "salesOrderInfo", method = RequestMethod.POST)
    public ResponseData<Page<SalesOrder>> salesList(@RequestParam(name = "pageNo") int pageIndex, @RequestParam(name = "pageSize") int pageSize,
                                                    @RequestParam(name = "name",required = false) String name , @RequestParam(name = "account",required = false) String account  ,
                                                    @RequestParam(name = "salesSubst",required = false) String salesSubst  , @RequestParam(name = "salesSchool",required = false) String salesSchool  ,
                                                    @RequestParam(name = "position",required = false) String position  , @RequestParam(name = "positionSJ",required = false) String positionSJ  ,
                                                    @RequestParam(name = "positionZJL",required = false) String positionZJL  , @RequestParam(name = "activateTimeStart",required = false) String activateTimeStart  ,
                                                    @RequestParam(name = "activateTimeEnd",required = false) String activateTimeEnd ){
         /* ShiroUser user = getShiroUser();
        if (!user.getRoleNames().stream().anyMatch(roleName -> StringUtils.equalsAny(roleName, "超级管理员"))) {
            return ResponseData.error("无数据权限");
        }*/
        Wrapper<SalesOrder> wrapper = new EntityWrapper<>();
        wrapper.ge("recently_amount",50);
        wrapper.eq(StringUtils.isNotBlank(name),"name",name);
        wrapper.eq(StringUtils.isNotBlank(name),"account",account);
        wrapper.eq(StringUtils.isNotBlank(name),"sales_subst",salesSubst);
        wrapper.eq(StringUtils.isNotBlank(name),"sales_school",salesSchool);
        if(StringUtils.isNotBlank(activateTimeEnd)&&StringUtils.isNotBlank(activateTimeStart)){
            activateTimeEnd=activateTimeEnd.substring(0,activateTimeEnd.length())+" 23:59:59";
            activateTimeStart=activateTimeStart.substring(0,activateTimeStart.length())+" 00:00:00";
        }
        wrapper.le(StringUtils.isNotBlank(activateTimeEnd),"activate_time",activateTimeEnd);
        wrapper.ge(StringUtils.isNotBlank(activateTimeStart),"activate_time",activateTimeStart);
        Page<SalesOrder> page = new Page<>(pageIndex, pageSize);
        List<SalesOrder> hhrSalesOrders = salesOrderMapper.selectPage(page, wrapper);
        List<SalesOrder> hos = new ArrayList<>();
        for (SalesOrder value:hhrSalesOrders){
            HhrUser hhrUser = hhrUserMapper.selectById(value.getUserId());
            if("总经理".equals(hhrUser.getPosition())){
                value.setPositionZJL(hhrUser.getPosition());
            }else {
                value.setPositionZW(hhrUser.getPosition());
                SysUser sysUser = sysUserMapper.selectById(hhrUser.getParentId());
                value.setPositionSJ(sysUser.getName());
            }
            hos.add(value);
        }

        page.setRecords(hos);
        return ResponseData.success(page, "查询成功！");

    }

}
