package com.jpsoft.shinestar.modules.business.controller;


import com.github.pagehelper.Page;
import com.jpsoft.shinestar.modules.base.entity.CompanyInfo;
import com.jpsoft.shinestar.modules.base.entity.CustomerDiningRecord;
import com.jpsoft.shinestar.modules.base.entity.PersonInfo;
import com.jpsoft.shinestar.modules.base.entity.PersonPopedom;
import com.jpsoft.shinestar.modules.base.service.CompanyInfoService;
import com.jpsoft.shinestar.modules.base.service.CustomerDiningRecordService;
import com.jpsoft.shinestar.modules.base.service.PersonInfoService;
import com.jpsoft.shinestar.modules.base.service.PersonPopedomService;
import com.jpsoft.shinestar.modules.common.dto.MessageResult;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.jpsoft.shinestar.modules.common.utils.PojoUtils;
import com.jpsoft.shinestar.modules.common.utils.TypeNameUtil;
import com.jpsoft.shinestar.modules.sys.entity.User;
import com.jpsoft.shinestar.modules.sys.service.UserService;
import com.jpsoft.shinestar.modules.workflow.entity.ProcinstActUser;
import com.jpsoft.shinestar.modules.workflow.entity.ProcinstActivity;
import io.swagger.annotations.ApiOperation;
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 javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/business/customerDining")
public class CustomerDiningController {

    @Autowired
    private CustomerDiningRecordService customerDiningRecordService;

    @Autowired
    private CompanyInfoService companyInfoService;

    @Autowired
    private PersonPopedomService personPopedomService;

    @Autowired
    private PersonInfoService personInfoService;

    @Autowired
    private UserService userService;


    @ApiOperation(value="列表")
    @RequestMapping(value = "pageList",method = RequestMethod.POST)
    public MessageResult<Map> pageList(
            String personName,String companyId,String auditStatus,String diningType,String createTimeRange,String diningTimeRange,String auditTimeRange,
            @RequestParam(value="pageIndex",defaultValue="1") int pageIndex,
            @RequestParam(value="pageSize",defaultValue="20") int pageSize,
            HttpServletRequest request){
        String subject = (String)request.getAttribute("subject");

        //当前用户ID
        System.out.println(subject);

        MessageResult<Map> msgResult = new MessageResult<>();

        Map<String,Object> searchParams = new HashMap<>();

        List<Sort> sortList = new ArrayList<>();
        sortList.add(new Sort("a.create_time","desc"));

        if (StringUtils.isNotEmpty(personName)) {
            searchParams.put("personName","%" + personName + "%");
        }

        User user = userService.get(subject);

        if (userService.hasRole(subject, "SYSADMIN")||userService.hasRole(subject, "食堂")) {
            if (StringUtils.isNotEmpty(companyId)) {
                searchParams.put("companyId", "%"+companyId+"%");
            }
        } else {
            if (StringUtils.isNotEmpty(companyId)) {
                searchParams.put("companyId", "%"+companyId+"%");
            }
            else {
                CompanyInfo companyInfo = companyInfoService.get(user.getCompanyId());
                searchParams.put("companyId", "%" + companyInfo.getId() + "%");
            }
        }

        if (StringUtils.isNotEmpty(auditStatus)) {
            searchParams.put("auditStatus",auditStatus);
        }

        if (StringUtils.isNotEmpty(diningType)) {
            if(diningType.equals("2")){
                searchParams.put("diningType","中餐");
            }
            else if(diningType.equals("3")){
                searchParams.put("diningType","晚餐");
            }
            else if(diningType.equals("4")){
                searchParams.put("diningType","宵夜");
            }
        }


        if (StringUtils.isNotEmpty(createTimeRange)) {
            String[] timeRangeArray = createTimeRange.split(",");
            String beginTime = "";
            String endTime = "";
            if (timeRangeArray.length == 1) {
                beginTime = timeRangeArray[0];
                beginTime+=" 00:00:00";
            } else if (timeRangeArray.length == 2) {
                beginTime = timeRangeArray[0];
                endTime = timeRangeArray[1];
                beginTime+=" 00:00:00";
                endTime+=" 23:59:59";
            }

            searchParams.put("beginTime", beginTime);
            searchParams.put("endTime", endTime);
        }

        if (StringUtils.isNotEmpty(auditTimeRange)) {
            String[] timeRangeArray = auditTimeRange.split(",");
            String beginTime = "";
            String endTime = "";
            if (timeRangeArray.length == 1) {
                beginTime = timeRangeArray[0];
                beginTime+=" 00:00:00";
            } else if (timeRangeArray.length == 2) {
                beginTime = timeRangeArray[0];
                endTime = timeRangeArray[1];
                beginTime+=" 00:00:00";
                endTime+=" 23:59:59";
            }

            searchParams.put("beginAuditTime", beginTime);
            searchParams.put("endAuditTime", endTime);
        }

        if (StringUtils.isNotEmpty(diningTimeRange)) {
            String[] timeRangeArray = diningTimeRange.split(",");
            String beginTime = "";
            String endTime = "";
            if (timeRangeArray.length == 1) {
                beginTime = timeRangeArray[0];
                beginTime+=" 00:00:00";
            } else if (timeRangeArray.length == 2) {
                beginTime = timeRangeArray[0];
                endTime = timeRangeArray[1];
                beginTime+=" 00:00:00";
                endTime+=" 23:59:59";
            }

            searchParams.put("beginDiningTime", beginTime);
            searchParams.put("endDiningTime", endTime);
        }

        Page<CustomerDiningRecord> page = customerDiningRecordService.pageSearch(searchParams,pageIndex,pageSize,true,sortList);

        for (CustomerDiningRecord customerDiningRecord:page) {
            CompanyInfo companyInfo = companyInfoService.get(customerDiningRecord.getCompanyId());
            if(companyInfo!=null){
                customerDiningRecord.setCompanyName(companyInfo.getName());
            }
            PersonPopedom personPopedom = personPopedomService.get(customerDiningRecord.getPersonPopedomId());
            if(personPopedom!=null){
                PersonInfo personInfo = personInfoService.get(personPopedom.getPersonId());
                if(personInfo!=null){
                    customerDiningRecord.setPersonName(personInfo.getName());
                }
            }
            customerDiningRecord.setStatusN(TypeNameUtil.getExamineTypeName(customerDiningRecord.getStatus()));
        }


        msgResult.setResult(true);
        msgResult.setData(PojoUtils.pageWrapper(page));

        return msgResult;
    }

}
